使用 CoreData 的一個簡單的方式:EasyCoreData

jopen 9年前發布 | 8K 次閱讀 iOS開發 移動開發 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

使用 CoreData 的一個簡單的方式:EasyCoreData

項目主頁:http://www.baiduhome.net/lib/view/home/1432022345286

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!