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

Categories

HOME
google-chrome
yii
seedstack
applescript
tabs
cryptography
repair
gprs
wagtail
vimeo
orange-api
pywinauto
hidden
word2vec
pickle
social-tables
sqlplus
nurbs
web-push
introspection
msdeploy
amazon-kms
aurelia-http-client
microkernel
right-click
game-center
suds
plane
1wire
contactless-smartcard
notesview
zend-form
destroy
jenkins-slave
intellij-lombok-plugin
ssi
django-static-precompiler
xcglogger
renderman
ptvs
http4s
finder
pbx
boost-compute
xmlbeans
clp
tsqlt
qtcpsocket
perforce-integrate
ajaxmin
maximize
gravatar
psd
onepage-checkout
perceptron
variable-length-array
fanotify
360-degrees
bootstrap-wysiwyg
erlog
scala-macros
maven-shade-plugin
device-admin
producer
git-reset
meteor-collections
webaii
cidr
cpu-time
pre
django-sites
iosched
application-blocks
saga
non-ascii-characters
ax
oscilloscope
background-thread
system-requirements
xalan
multiscaleimage
sfinae
viewpagerindicator
uitextfielddelegate
gamesalad
monocross
gwt-celltable
will-paginate
groovy-console
filelock
office-automation
azure-acs
directshow.net
ccl
explicit
ppc
datagridcolumn
word-automation

Resources

Encrypt Message