菜鸟教程小白 发表于 2022-12-12 14:49:22

ios - WatchKit 和 App 之间的 Core Data 实时同步


                                            <p><p>我正在为 iPhone 应用程序编写 WatchKit 扩展。我使用 App Groups 共享核心数据。这是有效的,数据是共享的。现在,如果应用程序在前台运行并且新的数据库条目从 watch 添加到数据库,我想刷新 iPhone 应用程序内的数据。跟踪我订阅了 <code>NSManagedObjectContextDidSaveNotification</code></p>

<pre><code>[ addObserver:self
                                                 selector:@selector(mergeChanges:)
                                                   name:NSManagedObjectContextDidSaveNotification
                                                   object:nil];
      }
    }
</code></pre>

<p>合并方法:</p>

<pre><code>- (void)mergeChanges:(NSNotification *)notification {
    if(notification.object != self.managedObjectContext) {
      ;
    }
}
</code></pre>

<p>更新主上下文方法:</p>

<pre><code>- (void)updateMainContext:(NSNotification *)notification {
    assert();
    ;
}
</code></pre>

<p>我试图在模拟器上测试它(通过同时运行 iPhone 和 Watch 模拟器),但不幸的是 <code>NSManagedObjectContextDidSaveNotification</code> 没有被调用。 </p>

<p>真的可以在模拟器上测试吗?如果是,这是正确的方法吗? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您不能使用 NSNotificationCenter,因为 iOS 应用程序和扩展程序是不同的进程。查看使用 MMWormhole 在进程之间发布您自己的通知。您可以在您的扩展程序和 iOS 应用程序中监听 <code>NSManagedObjectContextDidSaveNotification</code>,然后使用 MMWormhole 让其他进程知道通知。此外,您还需要手动刷新托管数据对象(参见 <code></code>)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - WatchKit 和 App 之间的 Core Data 实时同步,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/29265905/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/29265905/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - WatchKit 和 App 之间的 Core Data 实时同步