在今天扩展中创建记录并在应用中更新记录时的更改在今天扩展中不可见 [英] Changes when record was created in today extension and updated in app are not visible in today extension

查看:91
本文介绍了在今天扩展中创建记录并在应用中更新记录时的更改在今天扩展中不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我有一个应用和今天的扩展程序。我还有两个实体:目标服务

Simply I have an app, and today extension. Also I have two entities: Goal and Service.

@objc(Goal)
class Goal: NSManagedObject {

    @NSManaged var identifier: String
    @NSManaged var startDate: Date
    @NSManaged var endDate: Date

    private var services: [Service] {

        let predicate = NSPredicate(format: "date >= %@ && date <= %@", startDate as NSDate, endDate as NSDate)

        return Service.mr_findAll(with: predicate) as! [Service]
    }

    var duration: Int64 {
        return services.reduce(0) { $0 + $1.duration }
    }
}

当我创建与<$ c相关的服务时$ strong>今日扩展中的$ c>目标并在应用中对其进行更新,更新的结果在今天的扩展中不可见,而在应用中则可以正确显示。为什么?

When I create service related to goal in today extension and update it within an app, updated results are not visible in today extension while are displayed properly in an app. Why?

这是我在扩展或扩展中创建和更新记录的方式:

This is how I create and update record in an extension or in an extension:

MagicalRecord.save({ context in

    Service.createOrUpdate(withDuration: duration, date: Date(), identifier: identifier, in: context)

}, completion: { _, error in

        //do sth on completion
})


推荐答案

扩展程序和应用程序是共享某些相同代码的不同程序。因此,两者都创建了它们的 own persistentStoreCoordinator和上下文。

The extension and the app are different programs that share some of the same code. So both are creating their own persistentStoreCoordinator and context(s).

通常,在同一应用中,您会将多个上下文都附加到同一应用程序上persistentStoreCoordinator和其中的更改将由通过NotificationCenter发送并合并的通知处理。在这种情况下,存在两个问题-1)应用程序和扩展之间未共享notificationCenter,因此它们无法轻松通信; 2)上下文共享的 persistentStoreCoordinator 因此即使您传递了该信息也不能使用'mergeChangesFromContextDidSaveNotification`。

Normally in the same app you would have multiple context all attached to the same persistentStoreCoordinator and changes in one would be handled by the notifications that are sent out via notificationCenter and then merged. In this case there are two problems - 1) notificationCenter is not shared between the app and the extension so they can't communicate easily and 2) the context don't share the same persistentStoreCoordinator so you can't use 'mergeChangesFromContextDidSaveNotification` even if you did pass that information.

我以前没有尝试过,但这是我会尝试的方法:

I haven't tried this before, but this is what I would try:


  1. 以某种方式触发发生更改。有很多解决方案。

  2. 将您的 managedObjectContext 设置为具有 stalenessInterval 为0

  3. 当有更改时,您必须更新核心数据。您可以通过在所有核心数据对象上调用 refreshObject:mergeChanges:来执行此操作,或者执行另一个访存。诀窍在于,您必须从存储中获取上下文,并且必须从存储中获取文件中的上下文,并且不要在两者之间命中任何缓存。

  1. setup some way to trigger that a change occurred. There are many solutions to this.
  2. setup your managedObjectContext(s) to have a stalenessInterval of 0
  3. when there is a change you have to update your core data. You can do this by calling refreshObject:mergeChanges: on all your core data object, or do another fetch. The trick is that you have to have the context fetch from the store and the store to fetch from the file, and not hit any of the caches in the between.

这篇关于在今天扩展中创建记录并在应用中更新记录时的更改在今天扩展中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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