2013-05-13 62 views
2

我有一个应用程序,我已经与iCloud整合了核心数据。我在视图控制器中有以下通知。使用iCloud更新UITableView更新

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadFetchedResults:) name:NSPersistentStoreCoordinatorStoresDidChangeNotification object:coreDataController.psc]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadFetchedResults:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coreDataController.psc]; 

视图控制器包含装载有使用NSFetchedResultsController核心数据UITableView。我不确定要在以下方法中放置什么(由上述通知调用)来刷新表格。我尝试重新加载表格,并重新提交数据,但无济于事。我知道iCloud正在做它的工作,因为如果我完全重新加载视图控制器,更改后的数据显示。

- (void)reloadFetchedResults:(NSNotification*)note { 
    NSLog(@"Underlying data changed ... refreshing!"); 

    //_fetchedResultsController=nil; 
    //[self fetchedResultsController]; 

    [theTableView reloadData]; 


} 

任何帮助将不胜感激,谢谢!

+0

你居然刷新数据源?或者只是重新加载表? – Peres 2013-05-13 09:07:56

+0

嗯,我已经尝试调用'NSFetchResultsController'委托方法来重新获取数据,(注释掉了一点),但这也没有奏效。 – 2013-05-13 09:13:06

回答

4

您是否需要获取另一组数据,或者您是否希望看到对相同结果集的更改?如果前者,将其撕下并用新的谓词重新构建,获取等等,然后执行获取。

如果是后者,您需要将您的更改与您的moc合并。当您注册通知时,请将其设置为:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentContentsImportedChanges:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:self.managedObjectContext.persistentStoreCoordinator]; 


- (void) documentContentsImportedChanges:(NSNotification*)notification 
{ 
    // Received updates from iCloud 
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification]; 
} 
+0

谢谢!它是后者。 – 2013-05-15 08:52:09

+0

5竖起大拇指!真棒的答案,和一个奇妙的问题。为我工作,谢谢你们! – 2014-03-24 16:28:06

2

我同意Jason的回答。但是,您应该确保您在正确的线程中执行更新。

[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(persistentStoreDidImportUbiquitousContentChanges:) 
        name:NSPersistentStoreDidImportUbiquitousContentChangesNotification 
       object:_persistentStoreCoordinator]; 

- (void)persistentStoreDidImportUbiquitousContentChanges:(NSNotification*)note 
{ 
    NSManagedObjectContext *moc = self.managedObjectContext; 
    [moc performBlock:^{ 
     [moc mergeChangesFromContextDidSaveNotification:note]; 

    }]; 
} 

更新MOC的这种方法可以确保其在同一个线程上下文中完成,所以你没有任何崩溃;)