应用程序终止后,NSManagedObjectContext deleteObject无法正常工作

时间:2014-11-04 18:46:48

标签: ios core-data nsindexset

我的应用正在尝试从BE下载json数据并将其保存到核心数据中。为了跟踪要下载的剩余数据内容,我也将它们保存在核心数据中。当数据分批下载10时,我会从核心数据对象中删除下载的数据ID。它转到后台时工作正常。如果应用程序在数据下载期间被终止,则会在应用程序变为活动状态时恢复。但是下载后,我无法从NSManagedObjectContext中删除下载的数据ID。  我附上了以下代码:

- (void)removeIdListAtIndexesCustomized:(NSIndexSet *)indexes {
       NSMutableOrderedSet *IdListMutable = [self mutableOrderedSetValueForKey:@"IdList"];

      [indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
      [NSManagedObjectContext deleteObject:[IdListMutable objectAtIndex:idx]];//this is where the program stops
      }];
      NSError *error;
      if (![NSManagedObjectContext  save:&error]) {
         NSLog(@"Save failed: %@", [error localizedDescription]);
      }
 }

-(void)resumeSync{

   SyncData *syncData = [SyncData getSyncData]; //main thread
    NSInvocationOperation  *syncOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(getEvents:) object:syncData];
   [syncQueue addOperation:syncOperation];

}

-(void)getEvents:(SyncData *)syncData{

  //....

  [[DataManager sharedInstance] loadEvents:syncData];
  NSIndexSet *albumIndexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 15)];
  [syncData removeAlbumIdListAtIndexesCustomized:albumIndexSet];
}

DataManager.m

-(void)loadEvents:(SyncData *)data{
   EventRequest *eventRequest = [EventRequest new];
   eventRequest.albumData = postData;
   eventDataOperation = [[NSInvocationOperation alloc] initWithTarget:self
                                                            selector:@selector(loadEventRequest:)
                                                              object:eventRequest];
   // Add the operation to the queue and let it to be executed.
   [eventOperationQueue addOperation:eventDataOperation];
   if ([[eventOperationQueue operations] eventDataOperation]) {
      [eventDataOperation waitUntilFinished];
   }
  }

0 个答案:

没有答案