iOS Shared CoreData Storage for App Groups
Bài đăng này đã không được cập nhật trong 6 năm
Như bài viết trước mình đã giới thiệu về Today Extensions. Trong bài này chúng ta sẽ tiếp tục thực hiện các bước để lưu data cũng như cập nhật data cho app Today Extensions.
Đầu tiên bạn cần tạo một app groups cho ứng dụng của bạn. Truy cập vào Apple Developer Member Center và đăng ký một app group. Nhập thông tin và ID cho app groups
Tiếp theo bạn phản enable service App Groups, và update service trong Ap ID của bạn.
Edit App Groups và chọn App Group vừa tạo ra.
Tiếp theo, Download update Provisioning File. Chọn target của bạn và Enable App Groups trong xcode.
Sử dụng Core Data để lưu trữ data:
//
// CoreDataStorage.swift
// TutorialAppGroup
//
// Created by Apple House on 10/18/15.
// Copyright © 2015 Apple House. All rights reserved.
//
import Foundation
import CoreData
public class CoreDataStorage {
// MARK: - Shared Instance
class var sharedInstance : CoreDataStorage {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: CoreDataStorage? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = CoreDataStorage()
}
return Static.instance!
}
// MARK: - Initialization
init() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSavePrivateQueueContext:", name: NSManagedObjectContextDidSaveNotification, object: self.privateQueueCtxt)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSaveMainQueueContext:", name: NSManagedObjectContextDidSaveNotification, object: self.mainQueueCtxt)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
// MARK: - Notifications
@objc func contextDidSavePrivateQueueContext(notification: NSNotification) {
if let context = self.mainQueueCtxt {
self.synced(self, closure: { () -> () in
context.performBlock({() -> Void in
context.mergeChangesFromContextDidSaveNotification(notification)
})
})
}
}
@objc func contextDidSaveMainQueueContext(notification: NSNotification) {
if let context = self.privateQueueCtxt {
self.synced(self, closure: { () -> () in
context.performBlock({() -> Void in
context.mergeChangesFromContextDidSaveNotification(notification)
})
})
}
}
func synced(lock: AnyObject, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
// MARK: - Core Data stack
lazy var applicationDocumentsDirectory: NSURL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named 'Bundle identifier' in the application's documents Application Support directory.
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
return urls[urls.count-1]
}()
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = NSBundle.mainBundle().URLForResource("TutorialAppGroup", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
// Create the coordinator and store
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let directory = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.com.maximbilan.tutorialappgroup")!
let url = directory.URLByAppendingPathComponent("TutorialAppGroup.sqlite")
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
} catch var error as NSError {
coordinator = nil
NSLog("Unresolved error \(error), \(error.userInfo)")
abort()
} catch {
fatalError()
}
print("\(coordinator?.persistentStores)")
return coordinator
}()
// MARK: - NSManagedObject Contexts
public class func mainQueueContext() -> NSManagedObjectContext {
return self.sharedInstance.mainQueueCtxt!
}
public class func privateQueueContext() -> NSManagedObjectContext {
return self.sharedInstance.privateQueueCtxt!
}
lazy var mainQueueCtxt: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
var managedObjectContext = NSManagedObjectContext(concurrencyType:.MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator
return managedObjectContext
}()
lazy var privateQueueCtxt: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
var managedObjectContext = NSManagedObjectContext(concurrencyType:.PrivateQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator
return managedObjectContext
}()
// MARK: - Core Data Saving support
public class func saveContext(context: NSManagedObjectContext?) {
if let moc = context {
if moc.hasChanges {
do {
try moc.save()
} catch {
}
}
}
}
}
extension NSManagedObject {
public class func findAllForEntity(entityName: String, context: NSManagedObjectContext) -> [AnyObject]? {
let request = NSFetchRequest(entityName: entityName)
let result: [AnyObject]?
do {
result = try context.executeFetchRequest(request)
} catch let error as NSError {
print(error)
result = nil
}
return result
}
}
Bạn cần tạo 1 context và CoreData Object:
let context = CoreDataStorage.mainQueueContext()
var counter: Counter?
Để fetching data:
func fetchData() {
self.context.performBlockAndWait{ () -> Void in
let counter = NSManagedObject.findAllForEntity("Counter", context: self.context)
if (counter?.last != nil) {
self.counter = (counter?.last as! Counter)
}
else {
self.counter = (NSEntityDescription.insertNewObjectForEntityForName("Counter", inManagedObjectContext: self.context) as! Counter)
self.counter?.title = "Counter"
self.counter?.value = 0
}
self.updateUI()
}
}
Save Context:
CoreDataStorage.saveContext(self.context)
Kết Luận.
Trong bài này mình hướng dẫn các bạn cách sử dụng coredata để lưu data. Trong bài mình sử dụng Core data simple nhất, để thuận tiện khi sử dụng code data bạn có thể dùng Magic Record.
All rights reserved