vendredi 3 avril 2015

Accessing Shared Data from Two Apps

Do you know how do to share Core Data with two Apps (whose you're owner) allowing read and write to the same .sqlite ?


I have tried to use App Groups :


1) Bank.xcdatamodeld BankInfo.swift BankDetails.swift


I have copied those files into the directory for this app B's project (retrieve from the app A's project ) and then I have dragged them into Xcode.


2) I have retrieved the sqlite file from the common sandbox of the app groups


App Delegate : Core Data stack [Swift]



lazy var applicationDocumentsDirectory: NSURL = {

let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
return urls[urls.count-1] as NSURL
}()

lazy var managedObjectModel: NSManagedObjectModel = {

let modelURL = NSBundle.mainBundle().URLForResource("shareapps", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {


// Create the coordinator and store

var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

let directory = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("com.sd.shareapps");

let url = directory?.URLByAppendingPathComponent("shareapps.sqlite")

//Sarting frehs every time
NSFileManager.defaultManager().removeItemAtURL(url!, error: nil)

var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."

if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
println("\(coordinator?.persistentStores)")
return coordinator
}()


I have got this error message :



"*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name 'Entity'"


Regards


Aucun commentaire:

Enregistrer un commentaire