core-data
Convert NSArray to Swift Array
I’m currently using obj.types.allObjects as Type[], but that feels like a hack/workaround. Then revise your feelings. What you’re doing is exactly how you do “type-cast / convert the NSArray to Array<Type>[].” An NSArray always arrives into Swift as containing AnyObject (i.e. an Array<AnyObject>, aka AnyObject[]). Casting it down to a specific type, if you know … Read more
NSPersistentCloudKitContainer: How to check if data is synced to CloudKit
To quote “Framework Engineer” from a similar question in the Apple Developer forums: “This is a fallacy”. In a distributed system, you can’t truly know if “sync is complete”, as another device, which could be online or offline at the moment, could have unsynced changes. That said, here are some techniques you can use to … Read more
Is there a good Core Data browsing tool out there? [closed]
CoreDataEditor is $20 and doesn’t make it quick and easy to browse the relationships of the managed object context. There was also some insanity about having to have the MOM use exactly the same name as your application (??!), or somesuch. I’m not about to go in and start renaming stuff just to browse my … Read more
How do I get mogenerator to recognize the proper type for Transformable attributes?
This should be better documented. To set your generated attributes’s type, select your desired attribute in the modeler and switch to the User Info tab. Then create a new element with a key of attributeValueClassName and a value of whatever you’d like. Here’s a screenshot:
How to sync records between Core Data and CloudKit efficiently
As of iOS 13, there are new API’s that simplify this synchronization for developers. I would recommend you to watch the WWDC19 session about the new synchronization between CoreData and CloudKit. Please note that these new API’s only work for iOS 13+. Video: https://developer.apple.com/videos/play/wwdc2019/202/ In short, you need to start using NSPersistentCloudKitContainer instead of NSPersistentContainer. … Read more
Deleting all data in a Core Data entity in Swift 3
You’re thinking of NSBatchDeleteRequest, which was added in iOS 9. Create one like this: let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: “Employee”) let request = NSBatchDeleteRequest(fetchRequest: fetch) You can also add a predicate if you only wanted to delete instances that match the predicate. To run the request: let result = try managedObjectContext.executeRequest(request) Note that batch requests don’t … Read more
What does the Indexed Property of a CoreData attribute do?
I would recommend to read this on indexes: http://en.wikipedia.org/wiki/Index_(database). Simply put, a database engine creates a new structure which keeps the indexed column (which corresponds to a property) sorted and a link to the corresponding row for each entry (primary key). This allows for faster searches (since search in ordered lists is faster than in … Read more
Duplicate files in DerivedData folder using CoreData generator
I get this in Xcode 8.1 For me following steps solved the issue. Please note that order matters. 1) Create entity in Core Data model. 2) Under class section, make settings as on following image. Module: Current Product Name Codegen: Manual/None 3) Generate your NSManagedObject subclass.