xcode
How to generate a default record in an entity (Core Data)?
I'm just doing my first steps in Core Data and would like to change a piece of my code which uses a simple plist to store some preference data of a program. I would like to use Core Data instead. Just for educational reason. So basically I created an entity named "setup" with a bunch of attributes like "language", "debug info", "server ip" and so on. There should be only one data record in the entity as there is no sense to keep multiple setups. I then created the GUI in the interface builder by alt-dragging the entity over the GUI window. I choosed "single item view". I can run the code but there is a problem: I'm not able to change the content of the contols. I assume it's simply because there is no record in the entity. So my question is, how to create a "default" entry? I don't want to use the "Master/Detail view" as I simply need just one data record which should be generated automatically in case the storedata-file isn't created yet.
I solved the problem so far. I added the following lines to the method "- (NSManagedObjectContext *) managedObjectContext". It creates a default record in the entity on the first call of the method. The values of the attributes are set up in the data model. NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:#"Setup" inManagedObjectContext:managedObjectContext]]; [request setIncludesSubentities:NO]; //Omit subentities. Default is YES (i.e. include subentities) NSError *err; NSUInteger count = [managedObjectContext countForFetchRequest:request error:&err]; if(count==NSNotFound || count==0) { [NSEntityDescription insertNewObjectForEntityForName:#"Setup" inManagedObjectContext: managedObjectContext]; } [request release]; I'm not sure if it's the correct way to handle it, especially I doubt that's the right place. I'm missing the method which is called after the Core Data has been set up. Any suggestions are highly welcome!
Related Links
Xcode cannot find [NSRunningApplication terminate]
Xcode 8.0 builds all targets after any change to code
Xcode 8 upgrade errors
Is it possible to view source code WITHOUT rendering markdown in a .xcplaygroundpage?
xcode change output of console information [duplicate]
productbuild - Resources not copying
Copy the complete content of general NSPasteboard
XCode XCAssets and autocompletion
Xcode Folder references > missing files
How do you auto build custom included frameworks or “kits” in Xcode?
resubmitting to App Store, should I increase version number or build in xcode?
Code Signature Invalid
I can no longer comment code using Xcode 8.0
Stack guard causing crash C++ Memory allocation of Vector
How to find object by debug - address in xcode
Unable to boot simulator xcode 7.3.1 / OS 10.11.6