动作扩展和共享核心数据

动作扩展和共享核心数据

本文介绍了动作扩展和共享核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用Action扩展.

I am using the Action extension in my Application.

我们的应用当前使用核心数据.我在应用程序和使用应用程序组进行操作扩展,并成功将核心数据从文档目录迁移到应用程序组.

Our app currently uses Core Data. I am sharing this between the app and theAction Extension using App Groups and successfully migrated the Core Data from Documents Directory to App Group.

当我使用扩展程序将照片或便笺应用程序中的照片或便笺导入我的应用程序时,会遇到我面临的问题.该文档已成功导入,但无法在我的应用程序中查看该文档.仅当我终止该应用程序并再次运行它时,文档才会出现.

The problem which I am facing happens when importing photos or notes from Photos/Notes App into my application using the extension. The document is imported successfully but I am not able to view that document in my application. Only when I kill the application and run it again then the documents appear.

我在这里没有使用框架.所以我在同一商店的Application和Extension中有不同的代码文件.我使用了不同的托管对象上下文.即使我在执行动作扩展工作之后在应用程序中重新加载数据,项目还是不可见的,除非我杀死该应用程序并再次启动它.

I am not using a framework here. So I have different code files in Application and Extension using the same Store. I have used different Managed Object Contexts. Even though I am reloading the data in the application after performing Action Extension work, items are not visible unless I kill the application and launch it again.

可能是什么问题以及如何解决这个问题?

What could be the issue and how to resolve this?

推荐答案

您描述的问题之所以发生,是因为没有通知告诉您的应用程序持久性存储文件中有新数据,因此它只会显示现有数据.您需要自己处理.

The problem you describe happens because there's no notification to tell your app that there's new data in the persistent store file, so it just keeps showing the existing data. You need to handle this yourself.

一种简单的方法是在您的应用程序中观察UIApplicationDidBecomeActiveNotification.每当您收到该消息时,请重新加载您的数据.您将获得该扩展创建的所有新条目.

A simple approach is to observe UIApplicationDidBecomeActiveNotification in your app. Any time you receive that, reload your data. You'll get any new entries created by the extension.

一种更复杂的方法是使用 MMWormhole 之类的东西来通知应用新内容.然后,它将仅在实际存在的新条目时加载它们,而不是在每次新条目变为活动状态时重新加载.

A more sophisticated approach is to use something like MMWormhole to notify the app of new content. Then it would only load new entries when they actually exist, instead of reloading every time it becomes active.

这篇关于动作扩展和共享核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:12