upon loading the Root View Controller a Managed Object called Target should be created using the convenience method:
- (void)viewDidLoad {
[super viewDidLoad];
if (context == nil) {
context = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
if ([fetchedResultsController.fetchedObjects count] < 1) {
Target *aTarget = (Target *)[NSEntityDescription insertNewObjectForEntityForName:@"Target" inManagedObjectContext:context];
}
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
if(![context save:&error]){
//Handle error.
} ...
If I run the App on my iPhone from Xcode it works all fine. However, if I stop the test run and restart the App on the iPhone a new Managed Object is created even though the condition
[fetchedResultsController.fetchedObjects count] < 1)
does not seem to be satisfied (in the end it is, I just don’t know why). Once the App runs on the Phone independent of Xcode I can close and restart it without increasing the number of Managed Objects created. Only the first time after disconnecting it from Xcode.
Remark: The disconnection from Xcode causes the Segmentation fault: 11 error which also appears when I disconnect another Core Data App from Xcode. So I wouldn’t attribute this error to my problem.
Any ideas how to prevent the App from creating this additional object? Would you need more code to figure out the issue? Thanks a lot.
Solely based on the code I can see, I would think that this piece of code will create a new object the first time the code runs each time the app is run.
Try switching your 2nd and 3rd
ifstatements so that you’re performing the fetch first. I’d give you code but the code formatting is turning out to be a real pain to use from my iPad.