使用 CoreData 的一个简单的方式:EasyCoreData
EasyCoreData 提供一种在你的IOS工程中使用 CoreData 的一个简单的方式。这是一个 Swift cocoa touch框架。
Integration
Drag the EasyCoreData folder into your project. You can add sources only or add EasyCoreData project and use it as cocoa touch framework.
If you are doing integration with EasyCoreData as with Cocoa Touch framework do not forget to build EasyCoreData target before build and run your own project. To launch app which uses Cocoa Touch framework on the device you might need to add EasyCoreData.framework to 'embedded binaries' on Your_Project->General page
Setup
EasyCoreDatadon't required any setup. In most cases you may start to work as far as you have added it into your project. In some cases you may need to setupsqliteStoreURL,modelName,modelURL,modelBundleorpersistentStoreCoordinatorOptionsbefore start
Simply set values into EasyCoreData singleton class:
EasyCoreData.sharedInstance.modelName = "MyCustomModelName"
If you need to force seup all CoreData related properties you can callEasyCoreData.sharedInstance.setup()method, otherwise EasyCoreData will load all properties in lazy-load mode
Usage
- access main thread context:NSManagedObjectContext.mainThreadContext
- create tempolorary main thread context:NSManagedObjectContext.temporaryMainThreadContext
Save data in background: NSManagedObjectContext.saveDataInBackground({ localContext in for (index, dict) in enumerate(objects) { let album = localContext.createEntity(Album) album.mapFromJSONDict(dict, context: localContext) } }) { completion?() }
Create entity
let user = NSManagedObjectContext.mainThreadContext.createEntity(User)
orNSManagedObjectclass function, type cast is required
let user = User.createEntity() as! User
You can add additional paraminContextif needed here
Fetching objects let album = context.fetchFirstOne(Album) let songs = context.fetchObjects(Track.self, predicate: NSPreicate(format: "album == %@", album), sortDesriptors: [NSSortDescriptor(key: "sortingOrder", ascending: true)])
or
let album = Album.findFirstOne() as? Album let tracks = Track.findAll() as? [Track]
predicateandsortDescriptorsparams here are optional you can simply skip it
Asynchronous objects fetching (iOS 8+) context.fetchObjectsAsynchronously(Movie.self, predicate: NSPredicate("artist == 'Christopher Nolan'", sortDescriptors: [NSSortDescriptor(key: "releaseDate", ascending: false)]) { movies in }
Obtaining information about number of entities
User.countOfEntities() Track.hasAtLeastOneEntity(predicate: NSPredicate(format: "album == %@", album))
You can add additional paramspredicate,inContextif needed
Get entity from other context
let localObject = localContext.entityFromOtherContext(object)
or
let localUser = user.inContext(localContext) as? User
Aggregation operation
let value = Album.aggregateOperation("max:", onAttribute: "trackCount")?.intValue { println("Record track count in album is \(value)") }
you can addpredicateandinContextparameters if needed