Core Data — Building a Custom Store | by Avi Tsadok | Mar, 2022

Supply: undraw.co
  • Initialize our retailer.
  • Dealing with the fetching, together with predicates.
  • Managing the completely different contexts for us.
  • Create objects based mostly in your knowledge.
  • Generate object IDs.
  • Outline the shop metadata info.
  • Present extra knowledge info when requested.
class MyAtomicStore: NSAtomicStore
NSPersistentStoreCoordinator.registerStoreClass(MyAtomicStore.self, sort: MyAtomicStore.storeType)
var objectMapping = [UUID : NSManagedObjectID]()
override func load() throws {
  • Generate a brand new referenceID.
  • Generate a brand new ObjectID based mostly on the referenceID.
  • Create a brand new cache node (that’s our “object”).
  • Fill the cache node with knowledge.
  • Maps the objectID to its referenceID.
  • Add the cache node to the shop.
let objectID = self.objectID(for: songDesc, withReferenceObject: uuid)
cacheNode.setValue(identify, forKey: “identify”)
let albumCacheNode = NSAtomicStoreCacheNode(objectID: albumObjectID)// filling the albume node with knowledgesongNode.setValue(albumCacheNode, forKey: “album”)
if let albumNode = self.cacheNode(for: albumObjectID) cacheNode.setValue(albumNode, forKey: “album”) else // create a brand new album cache node from knowledge.
override func save() throws 
override func execute(_ request: NSPersistentStoreRequest, with context: NSManagedObjectContext?) throws -> Any

Fetching

Saving

func newValue(forRelationship relationship: NSRelationshipDescription, forObjectWith objectID: NSManagedObjectID, with context: NSManagedObjectContext?) throws -> Any

More Posts