WatchKit核心数据同步 [英] WatchKit Core Data Sync Up

查看:142
本文介绍了WatchKit核心数据同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构如下的应用程序

I have an app structured as follows

iOS应用程序将数据写入Core Data,该数据具有存储在共享应用程序组中的持久存储。

iOS App Writes data to Core Data which has a persistent store stored in a shared app group.

Watch Kit扩展程序能够读取由iOS应用程序编写的Core Data数据。

The Watch Kit extension is able to read data from Core Data that was written by the iOS app.

我遇到的问题是如果我的iOS应用程序在我的手表套件应用程序打开时写入数据,我不会得到更新,因为对象上下文与磁盘上的数据不同步。

The issue I am having is if my iOS app writes data while my watch kit app is open I am not getting updates because the object context is not syncing with the data on the disk.

因为我的手表套件扩展只读取数据,以便能够刷新上下文,并强制它从磁盘上的数据重新加载。

Is there a way that since my watch kit extension is only reading data to be able to refresh the context and force it to load again from the data on the disk?

推荐答案

我的工作解决方案使用 MMWormhole 发送通知( NSManagedObjectContextDidSaveNotification )从iPhone应用程序到我的手表应用程序。在手表应用程序的控制器中,我使用了NSManagedObjectContext的 mergeChangesFromContextDidSaveNotification:方法。

My working solution was using MMWormhole to send notification (NSManagedObjectContextDidSaveNotification) from iPhone app to my watch app. In the watch app's controller i used mergeChangesFromContextDidSaveNotification: method of NSManagedObjectContext.

// in iPhone app's notification handler
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"your.group.container.identifier" optionalDirectory:nil];
[wormhole passMessageObject:notification identifier:@"your notification identifier"];

// in WKInterfaceController's awakeWithContext: method
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"your.group.container.identifier" optionalDirectory:nil];
[wormhole listenForMessageWithIdentifier:@"your notification identifier" listener:^(id messageObject) {
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:messageObject];
}];

然后NSFetchedResultsController做了UI更新的所有其他工作。

Then NSFetchedResultsController did all other work with UI updates.

您必须为NSManagedObject子类实现NSCoding协议中的 initWithCoder: encodeWithCoder:方法,因为MMWormhole使用NSKeyedArchiver作为序列化媒体。

You must implement initWithCoder: and encodeWithCoder: methods from NSCoding protocol for your NSManagedObject subclasses because MMWormhole uses NSKeyedArchiver as a serialization medium.

- (id)initWithCoder:(NSCoder *)decoder {
    NSManagedObjectContext *context = ... // use your NSManagedObjectContext 
    NSPersistentStoreCoordinator *coordinator = ...; //use your NSPersistentStoreCoordinator
    NSURL *url = (NSURL *)[decoder decodeObjectForKey:@"URIRepresentation"];
    NSManagedObjectID *managedObjectID = [coordinator managedObjectIDForURIRepresentation:url];
    self = [context existingObjectWithID:managedObjectID error:nil];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:[[self objectID] URIRepresentation] forKey:@"URIRepresentation"];
}

这篇关于WatchKit核心数据同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆