Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read coredata from extension

Tags:

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")
    }
}
like image 392
radar Avatar asked Oct 10 '17 16:10

radar


1 Answers

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 {
    ...
}
like image 85
Tom Harrington Avatar answered Sep 29 '22 23:09

Tom Harrington