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

  • Initialize our store.
  • Handling the fetching, including predicates.
  • Managing the different contexts for us.
  • Create objects based on your data.
  • Generate object IDs.
  • Define the store metadata information.
  • Provide additional data information when requested.
class MyAtomicStore: NSAtomicStore
NSPersistentStoreCoordinator.registerStoreClass(MyAtomicStore.self, type: MyAtomicStore.storeType)
var objectMapping = [UUID : NSManagedObjectID]()
override func load() throws {
  • Generate a new referenceID.
  • Generate a new ObjectID based on the referenceID.
  • Create a new cache node (that’s our “object”).
  • Fill the cache node with data.
  • Maps the objectID to its referenceID.
  • Add the cache node to the store.
let objectID = self.objectID(for: songDesc, withReferenceObject: uuid)
cacheNode.setValue(name, forKey: “name”)
let albumCacheNode = NSAtomicStoreCacheNode(objectID: albumObjectID)// filling the albume node with datasongNode.setValue(albumCacheNode, forKey: “album”)
if let albumNode = self.cacheNode(for: albumObjectID) {cacheNode.setValue(albumNode, forKey: “album”)} else {// create a new album cache node from data.}
override func save() throws {}
override func execute(_ request: NSPersistentStoreRequest, with context: NSManagedObjectContext?) throws -> Any



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

Leave a Comment