无法从扩展程序读取coredata(Cannot read coredata from extension)

编程入门 行业动态 更新时间:2024-10-26 18:15:59
无法从扩展程序读取coredata(Cannot read coredata from extension)

我有一个应用程序,它将一些信息存储在coredata并读取它们。 我正在写这个应用程序的消息扩展,我想让这个扩展读取相同的数据,但我总是有空响应。

这是我在主应用程序中使用的代码:

context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext fetchImages(Date()){ (array, arrayData) in for image in array{ imagesArray.insert(image, at:0) } }

我在扩展中使用完全相同的代码,但它不读取数据。 我想知道的是我没有在代码中的任何地方使用appGroupIdentifier 。

我该怎么做才能实现这一目标?

谢谢。

这是fetchImages函数的代码:

func fetchImages(_ predicate:Date, completion:(_ array:[Image], _ arrayData:NSArray)->()){ var arrData = [NSManagedObject]() var existingImages = [Image]() let request :NSFetchrequest<NSFetchrequestResult> = NSFetchrequest(entityName: "Photo") do { let results = try context?.fetch(request) var myImage = Image() if ((results?.count) != nil) { for result in results! { myImage.imageUrl = (resultat as! NSManagedObject).value(forKey:"url") as! String myImage.imageFileName = (resultat as! NSManagedObject).value(forKey:"imageFileName") as! String existingImages.append(myImage) arrData.append(result as! NSManagedObject) } } else{ print ("No photo.") } completion(existingImages, arrData as NSArray) } catch{ print ("Error during CoreData request") } }

I have an app that stores some information in coredata and reads them. I'm writing a message extension of this application and I'd like to have this extension reading the same data but I always have empty response.

Here is the code I'm using in the main app:

context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext fetchImages(Date()){ (array, arrayData) in for image in array{ imagesArray.insert(image, at:0) } }

I'm using exactly the same code in the extension but it does not read the data. What I'm wondering about is that I'm not using the appGroupIdentifier anywhere in the code.

How can I do to achieve that?

Thanks.

Here is the code of fetchImages function:

func fetchImages(_ predicate:Date, completion:(_ array:[Image], _ arrayData:NSArray)->()){ var arrData = [NSManagedObject]() var existingImages = [Image]() let request :NSFetchrequest<NSFetchrequestResult> = NSFetchrequest(entityName: "Photo") do { let results = try context?.fetch(request) var myImage = Image() if ((results?.count) != nil) { for result in results! { myImage.imageUrl = (resultat as! NSManagedObject).value(forKey:"url") as! String myImage.imageFileName = (resultat as! NSManagedObject).value(forKey:"imageFileName") as! String existingImages.append(myImage) arrData.append(result as! NSManagedObject) } } else{ print ("No photo.") } completion(existingImages, arrData as NSArray) } catch{ print ("Error during CoreData request") } }

最满意答案

启用应用程序组是第一步,但现在您需要告诉Core Data使用应用程序组。

首先,从FileManager获取共享组容器的位置。 使用containerURL(forSecurityApplicationGroupIdentifier:)获取目录的文件URL。

如果需要,您可以使用该URL而无需更改。 在其中创建一个子目录以保存Core Data文件可能是个好主意。 如果这样做,请在URL上使用appendingPathComponent()方法为URL添加目录名称。 然后使用FileManager使用createDirectory(at:withIntermediateDirectories:attributes:)方法创建新目录。

既然您有一个共享目录,请告诉NSPersistentContainer将其文件放在那里。 您可以使用NSPersistentStoreDescription执行此NSPersistentStoreDescription 。 初始化程序可以使用一个URL来告诉它存储数据的位置。

您的代码将近似于此:

let directory: URL = // URL for your shared directory as described above let containerName: String = // Your persistent container name let persistentContainer = NSPersistentContainer(name: containerName) let persistentStoreDirectoryUrl = directory.appendingPathComponent(containerName) guard let _ = try? FileManager.default.createDirectory(at: persistentStoreDirectoryUrl, withIntermediateDirectories: true, attributes: nil) else { fatalError() } let persistentStoreUrl = persistentStoreDirectoryUrl.appendingPathComponent("\(containerName).sqlite") let persistentStoreDescription = NSPersistentStoreDescription(url: persistentStoreUrl) persistentContainer.persistentStoreDescriptions = [ persistentStoreDescription ] persistentContainer.loadPersistentStores { ... }

Turning on app groups is the first step, but now you need to tell Core Data to use the app group.

First you get the location of the shared group container, from FileManager. Use containerURL(forSecurityApplicationGroupIdentifier:) to get a file URL for the directory.

You can use that URL without changes if you want. It's probably a good idea to create a subdirectory in it to hold your Core Data files. If you do that, add a directory name to the URL with the appendingPathComponent() method on URL. Then use FileManager to create the new directory with the createDirectory(at:withIntermediateDirectories:attributes:) method.

Now that you have a shared directory to use, tell NSPersistentContainer to put its files there. You do that by using NSPersistentStoreDescription. The initializer can take a URL that tells it where to store its data.

Your code will be something approximating this:

let directory: URL = // URL for your shared directory as described above let containerName: String = // Your persistent container name let persistentContainer = NSPersistentContainer(name: containerName) let persistentStoreDirectoryUrl = directory.appendingPathComponent(containerName) guard let _ = try? FileManager.default.createDirectory(at: persistentStoreDirectoryUrl, withIntermediateDirectories: true, attributes: nil) else { fatalError() } let persistentStoreUrl = persistentStoreDirectoryUrl.appendingPathComponent("\(containerName).sqlite") let persistentStoreDescription = NSPersistentStoreDescription(url: persistentStoreUrl) persistentContainer.persistentStoreDescriptions = [ persistentStoreDescription ] persistentContainer.loadPersistentStores { ... }

更多推荐

本文发布于:2023-08-07 06:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   coredata   extension   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!