ios - 除非重新启动应用程序,否则核心数据应用程序中的 UITableView 不会更新第二个设备的主视图,并使用 iCloud 同步

标签 ios iphone core-data notifications icloud

亲爱的 Stack Overflow 的人们,

我已经使用 Core Data 和 iCloud 构建了一个应用程序。这是 UITabBarController 中的一个简单的 4 选项卡,每个选项卡都是一个 UITableViewController .第一个选项卡标题为时间轴,然后是人员、事件和日期。

使用设备 1,当“Using local storage: 0”显示在控制台中时,我添加了一些条目。当我建立第二个设备(在同一个 iCloud 帐户上)(iPad)时,它开始于 Using Local Storage: 1 (预期),然后是 0 (预期的)。但是,Timeline 选项卡(主 View )实际上并未显示新条目,但 Person、Event 和 Date 选项卡确实正确显示了它。如果我重新启动应用程序,它就会出现在时间轴中。

我正在使用 NSFetchedResultsControllerUITableViewController并引用了一个优秀的教程( http://pinkstone.co.uk/how-t-use-icloud-in-your-ios-apps/ ),在我的时间轴中,我添加了以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add iCloud observers
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(storesWillChange) name:NSPersistentStoreCoordinatorStoresWillChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];

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

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

}

#pragma mark - iCloud Methods

- (void)storesWillChange {

    NSLog(@"\n\nStores WILL change notification received\n\n");

    // disbale UI
    [[UIApplication sharedApplication]beginIgnoringInteractionEvents];

    // save and reset our context
    if (self.managedObjectContext.hasChanges) {
        [self.managedObjectContext save:nil];
    } else {
        [self.managedObjectContext reset];
    }
}

- (void)storesDidChange:(NSNotification *)notification {

    NSLog(@"\n\nStores DID change notification received\n\n");

    // enable UI
    [[UIApplication sharedApplication]endIgnoringInteractionEvents];

    // update UI
    [self.timelineTableView reloadData];
}

- (void)mergeContent:(NSNotification *)notification {

    NSLog(@"!Merge Content here!");
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}

我注意到一些奇怪的事情。

编辑清晰
1) iPad(第二台设备)在初始导入期间或任何后续同步期间从不运行 storesWillChange 或 storesDidChange。

2) iPad 的人员/事件选项卡已填充,但时间轴未填充。只有当我重新运行应用程序或从设备 1 添加新条目时,iPad 的时间线才会更新。

问题

我需要做什么才能让 iPad 在第一次连接到 iCloud 时在“时间轴”选项卡中显示数据?这不是耐心的问题,因为我等了 43 分钟没有任何变化。

当来自 iCloud 的数据很重要时,在第二台设备上运行的方法是什么?那是我这里的 key 。如果是storesDidChange,我应该把它放在哪里?我曾尝试将通知监听器放在 viewDidLoad 中时间轴 View Controller ,applicationDidFinishLaunchingWithOptions在 App Delegate 和 NSManagedObjectContext 中在任何这些情况下,它永远不会运行。

这是一个仅限 iOS 7 的应用程序。

编辑:进一步更新

我现在已经到了 iPad(第二台设备)仍然不显示 iCloud 数据的程度,但是如果我在第一台设备上添加一个新条目,它就会合并内容并同步。

今天早上看了很多遍苹果指南:https://developer.apple.com/library/prerelease/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW4 ,我可以看到在添加持久存储发生之前注册 storesDidChange 很重要。这是有道理的,我这样做了,我从 storesDidChange 方法中获取了 NSLog。

但是,在 One Time Ubiquity Store Container 完成设置后,它应该再次运行 storesWillChange 通知以及 storesDidChange。

在我的情况下,两者都没有运行。

引用此页面,这似乎正是我在这里面临的问题:http://www.zx81.org.uk/computing/programming/nsfetchedresultscontroller-and-icloud.html ,用户在UITableView中重新抓取。我正在这样做,但是,第一次运行并与 iCloud 商店同步时,数据不会出现在 iPad 上。

但是因为它是 UITabBarController ,其他选项卡正在适当更新。只是这个选项卡,它已经在从回退到 iCloud 商店迁移之前和之后显示。

编辑:添加 persistentStoreCoordinator方法在这里

如果问题出在我的persistentStoreCoordinator 上,我将在此处添加该代码:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    NSLog(@"Persistent Store");
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Envylope.sqlite"];

    NSError *error = nil;
    // get a reference to your iCloud URL
    NSURL *cloudURL = [self grabCloudPath:@"iCloud"];

    // setup options depending on migration
    NSDictionary *options = nil;
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"OnLatestVersion"]) {
        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES,
                                                           NSPersistentStoreUbiquitousContentURLKey: cloudURL, NSPersistentStoreUbiquitousContentNameKey: @"EnvyCloud"};
        NSLog(@"Using iCloud from migration");
    }
    else
    {
        NSLog(@"Using the other store without the iCloud Migration");
        options = @{                                       NSMigratePersistentStoresAutomaticallyOption : @YES,
                                                           NSInferMappingModelAutomaticallyOption : @YES};
    }

    NSLog(@"Just before adding the persistent store");
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    NSLog(@"Just after adding the store");

    return _persistentStoreCoordinator;
}

我有 if 语句和 userDefaults 的原因是因为我想检查用户的应用程序是否需要迁移到新版本,并在应用程序再次运行时维护该版本。我的意思是,如果我的设备上有 App Store 版本的数据并更新到这个 iCloud 版本,它会将数据迁移到 iCloud 商店。如果我再次运行该应用程序,它不会再次迁移,它只会使用 iCloud 商店。这可以同步,虽然我不认为这是问题所在(因为其他设备确实会在其他选项卡上获取云信息),但它在这里是为了完成。

任何通往正确方向的道路都会非常有帮助。

最佳答案

阿米特,

我已经做了一些挖掘,我相信你的应用程序的 iCloud 部分工作正常。问题似乎与获取的结果 Controller 有关 - 在某些设备上 - 需要重新获取。最简单的方法是在您收到 storesDidChange 通知时。而不是重新加载表 View ,尝试这样的事情:

- (void)storesDidChange:(NSNotification *)notification {

    NSLog(@"\n\nStores DID change notification received\n\n");

    // enable UI
    [[UIApplication sharedApplication]endIgnoringInteractionEvents];

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

}

首先,您将变量置零,然后再次调用自定义初始化程序(根据您的应用程序进行修改)。现在该表 View 应该可以正常更新并显示来自 iCloud 的结果。

关于那些丢失的通知:

您没有收到 willChange 和 didChange 的原因可能是因为您重新部署了应用程序,而没有先将其从设备中删除。这不是一个准确的场景:如果实际的商店文件没有改变,那么 iOS 将不会发布这些通知。通过在重新部署之前删除应用程序,这些通知会正确发布 - 并且希望也能更新您获取的结果 Controller 。

希望这可以帮助!

关于ios - 除非重新启动应用程序,否则核心数据应用程序中的 UITableView 不会更新第二个设备的主视图,并使用 iCloud 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24873458/

相关文章:

ios - 无法将 IBOutlet 链接到 XIB 文件 XCode 6

PHP 远程流式传输不适用于 iOS(Safari、Chrome)

ios:如何检测 UITextField 是否使用了语音听写?或者点击了键盘上的麦克风按钮

iphone - 是否可以设置 UIImageView 图像的位置?

iOS - 核心数据 - 表格 View 滚动到新添加的记录

ios - 使用 ARKit 时不显示 UILabel 文本

ios - 导出视频总是失败

javascript - 如何检测 iPhone 上页面滚动的比例?

ios - 当所有关系为零时如何自动删除任何实体?

iphone - 替换核心数据模型,无需迁移