在 Swift 或 Objective-C 中读取 Today Widget 中的 CoreData [英] Read CoreData in Today Widget in Swift or Objective-C

查看:19
本文介绍了在 Swift 或 Objective-C 中读取 Today Widget 中的 CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要一些简单的代码来帮助我如何在 TodayWidget 中读取 CoreData我做的这个步骤:

  • 使用 CoreData 创建新项目(在 AppDelegate 中)
  • 在 CoreData 模型中创建一个实体和一些属性,如名称或日期
  • 在 App 的 ViewController 中,用Martin"10/11"等一条记录填写姓名和日期

现在我需要在 TodayViewController 中读取这个名字

请在这里发布一些简单的代码,或者如何编辑 AppDelegate 以使用 appgroup ...

谢谢!

解决方案

小部件必须能够读取其代码中的数据.为此,您需要将 CoreData 文件设置为位于项目中的组"文件夹中(您可以设置 CoreData 堆栈以将其文件重新定位在此文件夹中).然后,您将在扩展中配置一个指向相同文件的 CoreData 堆栈.这些组文件夹是可以跨多个具有该组权限的目标/应用程序访问的文件夹.

我要指出的是,在小部件中使用和同步 NSUserDefaults 要容易得多,而且它是比 CoreData 更合适的应用程序.

您需要阅读以下 Apple 的关于共享扩展数据的文档:

Apple 文档

注意以下部分:

<块引用>

与包含的应用程序共享数据

即使应用程序扩展包嵌套在其包含的应用程序包,正在运行的应用程序扩展和包含应用程序没有直接访问彼此的容器.

背景

要了解容器,请阅读 File 中的 About the iOS File System系统编程指南.

但是,您可以启用数据共享.例如,您可能想要允许您的应用扩展程序及其包含的应用程序共享一个大量数据,例如预渲染资产.

I really need some simple code for me how to read CoreData in TodayWidget This steps I made:

  • Create new project with using CoreData (in AppDelegate)
  • Make an Entity in CoreData model and some Attributes like a name or date
  • in ViewController of App fill name and date with one record like "Martin" "10/11"

And Now I need to read this name in TodayViewController

Please really post here just some simple code from it, or how to edit AppDelegate for using appgroup ...

Thanks !

解决方案

Widgets have to be able to read the data in their code. In order to do this, you need to setup your CoreData files to be located in a 'group' folder in your project (You can set your CoreData Stack to relocate it's files in this folder). Then you'll configure a CoreData Stack in your extension that points to the same files. These group folders are folders that can be access across multiple targets/applications that have permissions for the group.

I will point out that it's far easier to use and sync NSUserDefaults across a widget and it's appropriate application than it is CoreData.

You'll want to read Apple's documentation about sharing extension data below:

Apple docs

Note the following section:

Sharing Data with Your Containing App

Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.

BACKGROUND

To learn about containers, read About the iOS File System in File System Programming Guide.

You can, however, enable data sharing. For example, you might want to allow your app extension and its containing app to share a single large set of data, such as prerendered assets.

这篇关于在 Swift 或 Objective-C 中读取 Today Widget 中的 CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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