Note that there are some explanatory texts on larger screens.

plurals
  1. POCoreData gets emptied after adding data into it
    primarykey
    data
    text
    <p>I have a CoreData entity <code>Tracker</code> which stores the dates. The app receives a notification and the <code>CheckListViewController</code> enters data in CoreData for up to 13 days, so when the CheckListViewController gets dismissed, the CoreData entity <code>Tracker</code> will be filled with 13 rows.</p> <p>In the <code>MainViewController</code> (which dismisses <code>CheckListViewController</code>), I have the following code:</p> <pre><code> - (void)dataSaved { self.checkListVC dismissViewControllerAnimated:YES completion:^{ // fetching all the data from 'Tracker' entity and doing NSLog on it // all data gets logged in console without any issues }]; } </code></pre> <p>Now, after that somewhere in my code, I fetch all the data from the entity <code>Tracker</code> but the return data is empty. The CoreData doesn't show any error it simply returns and empty array.</p> <p>Edit: Code to fetch results from CoreData</p> <pre><code>NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:ENTITY]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; [request setSortDescriptors:@[sortDescriptor]]; request.predicate = (fromDate &amp;&amp; toDate) ? [NSPredicate predicateWithFormat:@"date &gt;= %@ AND date &lt;= %@", fromDate, toDate] : nil; __block NSArray* fetchedHabits; [managedObjectContext performBlockAndWait:^{ NSError *error = nil; fetchedHabits = [managedObjectContext executeFetchRequest:request error:&amp;error]; if (error) NSLog(@"Unknown error occurred while fetching results from CoreData, %@ : %@", error, [error userInfo]); }]; </code></pre> <p>CoreData model: <img src="https://i.stack.imgur.com/l1nxA.png" alt="enter image description here"></p> <p><strong>Update 1</strong>: So as you can see there are two entities, namely <code>Habit</code> and <code>Tracker</code>. When I fetch results from <code>Habit</code> it all works fine, but when I try to fetch results from <code>Tracker</code> it gives me an empty array. I have a common <code>NSManagedObjectContext</code> instance because you can manage multiple CoreData entities with single <code>managedObjectContext</code>. I have checked <code>managedObjectContext.persistentStoreCoordinator.managedObjectModel.entitiesByName</code> and it also lists both the entities.</p> <p><strong>Update 2</strong>: Code where I add data in to <code>Tracker</code></p> <pre><code>TrackerCoreData *tracker = [NSEntityDescription insertNewObjectForEntityForName:ENTITY inManagedObjectContext:managedObjectContext]; tracker.date = date; tracker.habits = habits; // saving CoreData explicitly NSError *error = nil; [managedObjectContext save:&amp;error]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload