Slide 17
Slide 17 text
Basic Core Data Stack
1 model = NSManagedObjectModel.mergedModelFromBundles(nil)
2
3 store = NSPersistentStoreCoordinator.alloc.initWithManagedObjectModel(model)
4 store_path = File.join(NSHomeDirectory(), 'Documents', 'LearnXcdm.sqlite')
5 store_url = NSURL.fileURLWithPath(store_path)
6
7 options = { NSMigratePersistentStoresAutomaticallyOption => true,
8 NSInferMappingModelAutomaticallyOption => true }
9
10 error_ptr = Pointer.new(:object)
11
12 unless store.addPersistentStoreWithType(NSSQLiteStoreType,
13 configuration: nil,
14 URL: store_url,
15 options: options,
16 error: error_ptr)
17 raise "[ERROR] Failed to create persistent store: #{error_ptr[0].description}"
18 end
19
20 @context = NSManagedObjectContext.alloc.init
21 @context.persistentStoreCoordinator = store