NSExpression based Core Data fetch does not retrieve current values (iOS 5, GCD)

ⅰ亾dé卋堺 提交于 2019-12-22 07:24:12

问题


What I am trying to do

I am using the code below to download data (historic foreign exchange rates) from my backend server (parse.com) to my app's Core Data store.

The app checks for the latest available data stored locally and fetches only the newer data from the server. If there is no data stored locally yet, it fetches all data from the server.

The way the code is set up, it fetches the data in batches of 100 objects, saves the objects in Core Data, gets the new latest date for which data is now locally stored (by using NSExpression) and fetches the next batch until no more new objects are left on the server (objects.count = 0).

Because fetching is slow, I decided to run the fetching and Core Data saves on a back ground thread (using the new Core Data multi-treading model provided by iOS 5).

Fetching from the backend server works fine, but...

My Problem

it seems that only those objects are evaluated by the NSExpression which are stored on disk (physically in the database) and not the objects which are still in memory and will be saved soon. Hence, the fetch retrieves mostly the "old" values from disk (memory).

However, when using the following code of fetch (with no NSExpression and NSDictionary as result type), I get the current and correct values:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:localEntityName];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
request.sortDescriptors = @[sortDescriptor];
NSArray *results = [backgroundContext executeFetchRequest:request error:&error];
ForexHistory *forexHistoryItem = results.lastObject;
NSDate *lastLocalDate = forexHistoryItem.date;
NSLog(@"last local date results: %@",lastLocalDate);

What's wrong with my code below, which uses NSExpression and dictionary as the fetch resultType ?

My question

How can I make sure that the NSExpression which looks for the latest locally available date returns the latest date?

The code

- (void)seedForexHistoryInManagedObjectContext:(NSManagedObjectContext*)context {
  NSString* const localEntityName = @"ForexHistory";
  NSString* const parseEntityName = localEntityName;
  NSString* const parseDateIdentifier = @"date";

  NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"date"];
  NSExpression *maxPeriodExpression = [NSExpression expressionForFunction:@"max:"   
             arguments:@[keyPathExpression]];

  NSString *expressionName = @"maxDate";
  NSExpressionDescription *expressionDescription  = [[NSExpressionDescription alloc] init];
  expressionDescription.name                      = expressionName;
  expressionDescription.expression                = maxPeriodExpression;
  expressionDescription.expressionResultType      = NSDateAttributeType;

  NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:localEntityName];
  request.propertiesToFetch = @[expressionDescription];
  request.resultType = NSDictionaryResultType;

  NSArray *currencies = @[@"AUD",@"EUR",@"NZD",@"GBP",@"BRL",@"CAD",@"CNY"];

  dispatch_queue_t downloadQueue;
  downloadQueue = dispatch_queue_create("download", NULL); // create serial dispatch queue

  NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
  moc.parentContext = context;
  dispatch_async(downloadQueue,^{
     [moc performBlockAndWait:^{
          NSArray *objects;
             do {
               NSError *error;
               NSArray *dateResults = [moc executeFetchRequest:request error:&error];
               NSAssert(dateResults.count == 1,@"Request error!");
               NSDate *lastLocalDate = dateResults.lastObject[expressionName];
               NSLog(@"last local date results: %@",lastLocalDate);

               PFQuery *query = [PFQuery queryWithClassName:parseEntityName];
               query.limit = 100;
               [query orderByAscending:parseDateIdentifier];

               if (lastLocalDate) [query whereKey:parseDateIdentifier greaterThan:lastLocalDate]; // else query all

                objects = [query findObjects];

                [objects enumerateObjectsUsingBlock:^(PFObject *obj, NSUInteger idx, BOOL *stop) {

                ForexHistory *forexHistory = [NSEntityDescription    insertNewObjectForEntityForName:localEntityName
                                                                                   inManagedObjectContext:moc];
                        forexHistory.date = NULL_TO_NIL(obj[@"date"]);

                        [currencies enumerateObjectsUsingBlock:^(NSString *currency, NSUInteger idx, BOOL *stop) {
                            [forexHistory setValue:NULL_TO_NIL(obj[currency]) forKey:currency.lowercaseString];
                        }];
                    }];
                    NSError *saveError = nil;
                    [moc save:&saveError];

                    if (!saveError) NSLog(@"%lu forex rates saved successfully.",(unsigned long)objects.count);
                    else NSLog(@"Error when downloading historic forex rates: %@",error.localizedDescription);

               } while (objects.count > 0);
          }];
}

Thank you for your help!


回答1:


Unfortunately, this is not possible. See the documentation of setIncludesPendingChanges::

Special Considerations

A value of YES is not supported in conjunction with the result type NSDictionaryResultType, including calculation of aggregate results (such as max and min). For dictionaries, the array returned from the fetch reflects the current state in the persistent store, and does not take into account any pending changes, insertions, or deletions in the context.




回答2:


Include pending changes: Insert [request setIncludePendingChanges:YES]; before you do the fetch.

This ensures that unsaved changes are included in the fetch.



来源:https://stackoverflow.com/questions/14010809/nsexpression-based-core-data-fetch-does-not-retrieve-current-values-ios-5-gcd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!