Note that there are some explanatory texts on larger screens.

plurals
  1. POInserted object is not reflected in the query results with NSFetchRequest
    primarykey
    data
    text
    <p>I've just started playing with the iOS development and Core Data technology.</p> <p>What I am trying to do is to insert object into the Core Data and then run fetch request within same context looking for the newly inserted object.</p> <p>Here is the code I am using to insert the object:</p> <pre><code>+(Reward*) rewardForAction: (Actions) action inManagedObjectContext: (NSManagedObjectContext *) context { NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Reward"]; NSPredicate *actionPredicate = [NSPredicate predicateWithFormat:@"action = %@", [NSString stringWithFormat:@"%i", action]]; NSPredicate *todayPredicate = [NSPredicate predicateWithFormat:@"when &gt; %@", [NSDate today]]; NSLog(@"Today's midnight :%@", [NSDate today]); request.predicate = [NSCompoundPredicate andPredicateWithSubpredicates: [NSArray arrayWithObjects: actionPredicate, todayPredicate, nil]]; NSError *error = nil; NSArray *matches = [context executeFetchRequest: request error: &amp;error]; Reward *reward = nil; if(!matches) { NSLog(@"ERROR: failed to retrieve rewards"); } else if(matches.count &gt; 0) { NSLog(@"WRONG: reward already exists"); } else { reward = [NSEntityDescription insertNewObjectForEntityForName:@"Reward" inManagedObjectContext:context]; reward.action = [NSNumber numberWithInt:action]; reward.when = [NSDate date]; reward.pointsEarned = [Reward getPointsForAction:action]; } [Stats track: context]; [context save:nil]; return reward; } </code></pre> <p>This code first checks if reward is already given today for some action and if not it gives a reward. </p> <p>calling this method twice with same parameters, like this</p> <p>[Reward rewardForAction:APPLICATION_LAUNCH inManagedObjectContext: self.db.managedObjectContext]; [Reward rewardForAction:APPLICATION_LAUNCH inManagedObjectContext: self.db.managedObjectContext];</p> <p>expected to result in inserting only one object.</p> <p>But, in fact it inserts two objects into the Core Data. In the debugger I see second fetch request returns no objects when one is already there.</p> <p>Looks like NSFetchRequest does not see changes in the data store and operating will on old data. Am I missing something ? </p> <p><strong>EDIT:</strong> I have also set up observer for the NSManagedObjectContextObjectsDidChangeNotification and when it gets called I am calculating sum of the points to reflect it on the UI. Unfortunately sum also does not contain points from the inserted object. Here is my code to calculate sum:</p> <pre><code>+(NSInteger) getPoints: (NSManagedObjectContext *) context { Stats * stats= [Stats get: context]; NSArray *args = [NSArray arrayWithObject: [NSExpression expressionForKeyPath:@"pointsEarned"]]; NSExpression *ex = [NSExpression expressionForFunction:@"sum:" arguments:args]; NSExpressionDescription *ed = [[NSExpressionDescription alloc] init]; [ed setName:@"result"]; [ed setExpression:ex]; [ed setExpressionResultType:NSInteger32AttributeType]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setPropertiesToFetch:[NSArray arrayWithObject:ed]]; [request setResultType:NSDictionaryResultType]; /*if([stats intervalStartDate]) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"when &gt;= %@", [stats intervalStartDate]]; [request setPredicate:predicate]; }*/ NSEntityDescription *entity = [NSEntityDescription entityForName:@"Reward" inManagedObjectContext:context]; [request setEntity:entity]; NSArray *results = [context executeFetchRequest:request error:nil]; if(results &amp;&amp; results.count &gt; 0) { NSDictionary *resultsDictionary = [results objectAtIndex:0]; NSNumber *resultValue = [resultsDictionary objectForKey:@"result"]; return [resultValue intValue]; } else { return 0; } } </code></pre> <p>Strangely enough when I stop the app on the simulator and run it again. I can see all points as I expect.</p>
    singulars
    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