在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tadija/AERecord开源软件地址(OpenSource Url):https://github.com/tadija/AERecord开源编程语言(OpenSource Language):Swift 98.6%开源软件介绍(OpenSource Introduction):
AERecordSuper awesome Swift minion for Core Data (iOS, macOS, tvOS)
IndexIntroAECoreDataUI was previously part of AERecord, so you may want to check that also. Why do we need yet another one Core Data wrapper? You tell me! Features
UsageYou may see this demo project for example. Create Core Data stackAlmost everything in do {
try AERecord.loadCoreDataStack()
} catch {
print(error)
} or like this: let myModel: NSManagedObjectModel = AERecord.modelFromBundle(for: MyClass.self)
let myStoreType = NSInMemoryStoreType
let myConfiguration = ...
let myStoreURL = AERecord.storeURL(for: "MyName")
let myOptions = [NSMigratePersistentStoresAutomaticallyOption : true]
do {
try AERecord.loadCoreDataStack(managedObjectModel: myModel, storeType: myStoreType, configuration: myConfiguration, storeURL: myStoreURL, options: myOptions)
} catch {
print(error)
} or any combination of these. If for any reason you want to completely remove your stack and start over (separate demo data stack for example) you can do it as simple as this: do {
try AERecord.destroyCoreDataStack() // destroy default stack
} catch {
print(error)
}
do {
let demoStoreURL = AERecord.storeURL(for: "Demo")
try AERecord.destroyCoreDataStack(storeURL: demoStoreURL) // destroy custom stack
} catch {
print(error)
} Similarly you can delete all data from all entities (without messing with the stack) like this: AERecord.truncateAllData() Context operationsContext for current thread ( // get context
AERecord.Context.main // get NSManagedObjectContext for main thread
AERecord.Context.background // get NSManagedObjectContext for background thread
AERecord.Context.default // get NSManagedObjectContext for current thread
// execute NSFetchRequest
let request = ...
let managedObjects = AERecord.execute(fetchRequest: request) // returns array of objects
// save context
AERecord.save() // save default context
AERecord.saveAndWait() // save default context and wait for save to finish
// turn managed objects into faults (you don't need this often, but sometimes you do)
let objectIDs = ...
AERecord.refreshObjects(with: [objectIDs], mergeChanges: true) // turn objects for given IDs into faults
AERecord.refreshRegisteredObjects(mergeChanges: true) // turn all registered objects into faults Easy QueriesEasy querying helpers are created as GeneralIf you need custom // create request for any entity type
let attributes = ...
let predicate = NSManagedObject.createPredicate(with: attributes)
let sortDescriptors = ...
let request = NSManagedObject.createFetchRequest(predicate: predicate, sortDescriptors: sortDescriptors)
// set some custom request properties
request.someProperty = someValue
// execute request and get array of entity objects
let managedObjects = AERecord.execute(fetchRequest: request) Of course, all of the often needed requests for creating, finding, counting or deleting entities are already there, so just keep reading. CreateNSManagedObject.create() // create new object
let attributes = ...
NSManagedObject.create(with: attributes) // create new object and sets it's attributes
NSManagedObject.firstOrCreate(with: "city", value: "Belgrade") // get existing object (or create new if it doesn't already exist) with given attribute
let attributes = ...
NSManagedObject.firstOrCreate(with: attributes) // get existing object (or create new if it doesn't already exist) with given attributes Find firstNSManagedObject.first() // get first object
let predicate = ...
NSManagedObject.first(with: predicate) // get first object with predicate
NSManagedObject.first(with: "bike", value: "KTM") // get first object with given attribute name and value
let attributes = ...
NSManagedObject.first(with: attributes) // get first object with given attributes
NSManagedObject.first(orderedBy: "speed", ascending: false) // get first object ordered by given attribute name Find allNSManagedObject.all() // get all objects
let predicate = ...
NSManagedObject.all(with: predicate) // get all objects with predicate
NSManagedObject.all(with: "year", value: 1984) // get all objects with given attribute name and value
let attributes = ...
NSManagedObject.all(with: attributes) // get all objects with given attributes Deletelet managedObject = ...
managedObject.delete() // delete object (call on instance)
NSManagedObject.deleteAll() // delete all objects
NSManagedObject.deleteAll(with: "fat", value: true) // delete all objects with given attribute name and value
let attributes = ...
NSManagedObject.deleteAll(with: attributes) // delete all objects with given attributes
let predicate = ...
NSManagedObject.deleteAll(with: predicate) // delete all objects with given predicate CountNSManagedObject.count() // count all objects
let predicate = ...
NSManagedObject.count(with: predicate) // count all objects with predicate
NSManagedObject.count(with: "selected", value: true) // count all objects with given attribute name and value
let attributes = ...
NSManagedObject.count(with: attributes) // count all objects with given attributes Distinctdo {
try NSManagedObject.distinctValues(for: "city") // get array of all distinct values for given attribute name
} catch {
print(error)
}
do {
let attributes = ["country", "city"]
try NSManagedObject.distinctRecords(for: attributes) // get dictionary with name and values of all distinct records for multiple given attributes
} catch {
print(error)
} Auto IncrementIf you need to have auto incremented attribute, just create one with Int type and get next ID like this: NSManagedObject.autoIncrementedInteger(for: "myCustomAutoID") // returns next ID for given attribute of Integer type Turn managed object into fault
let managedObject = ...
managedObject.refresh() // turns instance of managed object into fault Batch updateBatch updating is the 'new' feature from iOS 8. It's doing stuff directly in persistent store, so be carefull with this and read the docs first. Btw, NSManagedObject.batchUpdate(properties: ["timeStamp" : NSDate()]) // returns NSBatchUpdateResult?
NSManagedObject.objectsCountForBatchUpdate(properties: ["timeStamp" : NSDate()]) // returns count of updated objects
NSManagedObject.batchUpdateAndRefreshObjects(properties: ["timeStamp" : NSDate()]) // turns updated objects into faults after updating them in persistent store Installation
LicenseAERecord is released under the MIT license. See LICENSE for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论