problems saving to persistent storage iphone - iphone

I am writing an iphone app; I need to save the data to persistent storage, but I am having the problems...
I created an entity in xcdatamodel and I have no problem getting the default value set for my attribute. In my testing, I go thru the code several times below and can change this attribute (it's an integer) while the application is active. However, when I close and reopen the app, my integer (myInt) is back to the default value when I expected it to be the last value I updated it to.
Here is what I am doing:
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *myTestEntity = [NSEntityDescription
entityForName:#"TestEntity"
inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:timeStampEntity];
NSError *error = nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// TODO: Handle the error
}
for (id oneObject in fetchedObjects)
{
NSInteger myInt = [[oneObject valueForKey:#"myStoredInt"]intValue];
myInt++;
[oneObject setValue:[NSNumber numberWithInt: myInt] forKey:#"myStoredInt"];
NSError *error = nil;
if (![managedObjectContext save:&error]) {//TODO: Handle error
}
}
[fetchRequest release];
I was hoping that line:
if (![managedObjectContext save:&error])
would do the job and store the changes to the persistent storage, but it does not! (Having this line inside or outside the for loop does not change the result)
What am I doing wrong?
Thank you very much for your help!

It will be helpful if you could show your app delegate code. My guess is that somewhere you are reseting the data your self. maybe in the method that you load the start data.
3 notes for now:
you don't need to place the save
method inside the loop. you should
place it after all the changes are
made and just once.
check if you have a save method called in the (if not add them):
- (void)applicationWillTerminate:(UIApplication*)application {
[self saveContext];
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
[self saveContext];
}
if it dose not help try to see if you are loading the default values on every lunch of the app.
GOOD LUCK
and welcome
EDIT
Hey, you can do that -
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"anyname.sqlite"]];
//check if the store already exist?
BOOL firstRun = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path] isDirectory:NULL]) {
firstRun = YES;
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
if(firstRun){
//add your default data here
}
}

Related

How can I control when iCloud syncs my core data?

I have the following code in place to sync my core data with iCloud. I only want it to sync when I say, however. For example, to only sync when on wifi. Would I be able to do this? Here is my code:
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[moc performBlockAndWait:^{
[moc setPersistentStoreCoordinator: coordinator];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];
}];
managedObjectContext = moc;
managedObjectContext.mergePolicy = [[NSMergePolicy alloc]
initWithMergeType:NSMergeByPropertyObjectTrumpMergePolicyType];
}
return managedObjectContext;
}
- (void)mergeChangesFrom_iCloud:(NSNotification *)notification {
NSLog(#"Merging in changes from iCloud...");
NSManagedObjectContext* moc = [self managedObjectContext];
[moc performBlock:^{
[moc mergeChangesFromContextDidSaveNotification:notification];
NSNotification* refreshNotification = [NSNotification notificationWithName:#"SomethingChanged"
object:self
userInfo:[notification userInfo]];
[[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
}];
}
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:#"EntryDatabase" ofType:#"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if((persistentStoreCoordinator != nil)) {
return persistentStoreCoordinator;
}
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSPersistentStoreCoordinator *psc = persistentStoreCoordinator;
// Set up iCloud in another thread:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// ** Note: if you adapt this code for your own use, you MUST change this variable:
NSString *iCloudEnabledAppID = #"iCloud ID is here, i removed it from stack overflow";
// ** Note: if you adapt this code for your own use, you should change this variable:
NSString *dataFileName = #"CoreDataStore.sqlite";
// ** Note: For basic usage you shouldn't need to change anything else
NSString *iCloudDataDirectoryName = #"Data.nosync";
NSString *iCloudLogsDirectoryName = #"Logs";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:dataFileName];
NSURL *localStore = [NSURL fileURLWithPath:storePath];
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
if (iCloud) {
NSLog(#"iCloud is working");
NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];
NSLog(#"iCloudEnabledAppID = %#",iCloudEnabledAppID);
NSLog(#"dataFileName = %#", dataFileName);
NSLog(#"iCloudDataDirectoryName = %#", iCloudDataDirectoryName);
NSLog(#"iCloudLogsDirectoryName = %#", iCloudLogsDirectoryName);
NSLog(#"iCloud = %#", iCloud);
NSLog(#"iCloudLogsPath = %#", iCloudLogsPath);
if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
NSError *fileSystemError;
[fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
withIntermediateDirectories:YES
attributes:nil
error:&fileSystemError];
if(fileSystemError != nil) {
NSLog(#"Error creating database directory %#", fileSystemError);
}
}
NSString *iCloudData = [[[iCloud path]
stringByAppendingPathComponent:iCloudDataDirectoryName]
stringByAppendingPathComponent:dataFileName];
NSLog(#"iCloudData = %#", iCloudData);
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[options setObject:iCloudEnabledAppID forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:iCloudLogsPath forKey:NSPersistentStoreUbiquitousContentURLKey];
[psc lock];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[NSURL fileURLWithPath:iCloudData]
options:options
error:nil];
[psc unlock];
}
else {
NSLog(#"iCloud is NOT working - using a local store");
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[psc lock];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:localStore
options:options
error:nil];
[psc unlock];
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"SomethingChanged" object:self userInfo:nil];
});
});
return persistentStoreCoordinator;
}
So far as I can tell, this can't happen. Not in any simple and efficient manner anyway.
http://developer.apple.com/library/mac/#documentation/General/Conceptual/iCloudDesignGuide/Chapters/iCloudFundametals.html
The problem is that the operations of iCloud are not transparent. Thus iOS decides when a sync will occur and how often. in the document above, Apple says that "best practice" is to use the sandbox exclusively or the iCloud exclusively, but don't copy your data between them.
That said, apple makes a reference to operating when iCloud becomes unavailable. This seems to fit the scenario that you are suggesting. Fundamentally, you would need to set it up as normal, ensure you have the data you need, then deactivate iCloud and move your data to the sandbox to work on it. When you want to sync, you just copy the data back.
To handle changes in iCloud availability, implement a method to be invoked on receiving an NSUbiquityIdentityDidChangeNotification notification. Your method needs to perform the following work:
Call the ubiquityIdentityToken method and store its return value.
Compare the new value to the previous value, to find out if the user signed out of their account or signed in to a different account.
If the previously-used account is now unavailable, save the current state locally as needed, empty your iCloud-related data caches, and refresh all iCloud-related user interface elements.
If you want to allow users to continue creating content with iCloud unavailable, store that content in your app’s sandbox container. When the account is again available, move the new content to iCloud. It’s usually best to do this without notifying the user or requiring any interaction from the user.
This would seem to match your requirements. However, I am only just reaching this stage myself, so I cannot comment further.

iOS core data store in iCloud to sync with other devices

I've been looking at getting Core Data working with iCloud so peoples data is accessible from multiple devices.
I've been following this tutorial (http://timroadley.com/) best as possible with chugging my app, but i've ran into a few issues.
Issue 1: When the app is closed (not just suspended) and reopened there is nothing showing in the table, so the previous data doesn't seem to get loaded
Issue 2: I don't know if its iCloud or the app, but it takes age to sync the changes. I make a change on one device, its takes 2-3 minutes to show on the other
Can anyone help me solve these two issues please
This code is in my AppDelegate
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if((__persistentStoreCoordinator != nil)) {
return __persistentStoreCoordinator;
}
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSPersistentStoreCoordinator *psc = __persistentStoreCoordinator;
// Set up iCloud in another thread:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// ** Note: if you adapt this code for your own use, you MUST change this variable:
NSString *iCloudEnabledAppID = #"MF4HVVX5DS.Desbrina.Medicine-Tracker";
// ** Note: if you adapt this code for your own use, you should change this variable:
NSString *dataFileName = #"Medicine-Tracker.sqlite";
// ** Note: For basic usage you shouldn't need to change anything else
NSString *iCloudDataDirectoryName = #"Data.nosync";
NSString *iCloudLogsDirectoryName = #"Logs";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
if (iCloud) {
NSLog(#"iCloud is working");
NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];
NSLog(#"iCloudEnabledAppID = %#",iCloudEnabledAppID);
NSLog(#"dataFileName = %#", dataFileName);
NSLog(#"iCloudDataDirectoryName = %#", iCloudDataDirectoryName);
NSLog(#"iCloudLogsDirectoryName = %#", iCloudLogsDirectoryName);
NSLog(#"iCloud = %#", iCloud);
NSLog(#"iCloudLogsPath = %#", iCloudLogsPath);
if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
NSError *fileSystemError;
[fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
withIntermediateDirectories:YES
attributes:nil
error:&fileSystemError];
if(fileSystemError != nil) {
NSLog(#"Error creating database directory %#", fileSystemError);
}
}
NSString *iCloudData = [[[iCloud path]
stringByAppendingPathComponent:iCloudDataDirectoryName]
stringByAppendingPathComponent:dataFileName];
NSLog(#"iCloudData = %#", iCloudData);
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[options setObject:iCloudEnabledAppID forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:iCloudLogsPath forKey:NSPersistentStoreUbiquitousContentURLKey];
[psc lock];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:[NSURL fileURLWithPath:iCloudData]
options:options
error:nil];
[psc unlock];
}
else {
NSLog(#"iCloud is NOT working - using a local store");
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[psc lock];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:localStore
options:options
error:nil];
[psc unlock];
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:#"SomethingChanged" object:self userInfo:nil];
});
});
return __persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[moc performBlockAndWait:^{
[moc setPersistentStoreCoordinator: coordinator];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(mergeChangesFrom_iCloud:) name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:coordinator];
}];
__managedObjectContext = moc;
}
return __managedObjectContext;
}
- (void)mergeChangesFrom_iCloud:(NSNotification *)notification {
NSLog(#"Merging in changes from iCloud...");
NSManagedObjectContext* moc = [self managedObjectContext];
[moc performBlock:^{
[moc mergeChangesFromContextDidSaveNotification:notification];
NSNotification* refreshNotification = [NSNotification notificationWithName:#"SomethingChanged"
object:self
userInfo:[notification userInfo]];
[[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
}];
}
and this is in the master view controller
- (void)reloadFetchedResults:(NSNotification*)note {
NSLog(#"Underlying data changed ... refreshing!");
[self fetchedResultsController];
NSLog(#"%#", note.object);
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadFetchedResults:)
name:#"SomethingChanged"
object:[[UIApplication sharedApplication] delegate]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Issue 1: When the app is closed (not just suspended) and reopened there is nothing showing in the table, so the previous data doesn't seem to get loaded
check that you save all data to your managed object context. This should not be Core Data issue
Issue 2: I don't know if its iCloud or the app, but it takes age to sync the changes. I make a change on one device, its takes 2-3 minutes to show on the other
2-3 minutes is really fine. It can sometimes take hours to get data in fully synced state. At least I experienced this half a year ago.
Bear in mind that iCloud is not suited for simultaneous use on several devices. It rather offers something like eventual consistency.
in your example:
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
use your
NSString *iCloudEnabledAppID = #"MF4HVVX5DS.Desbrina.Medicine-Tracker";
as URLForUbiquityContainerIdentifier
it should be like that:
NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier: iCloudEnabledAppID];

Core Data not persisting in iOS 5

V. iOS 5
I have my Model2.xcdatamodelId containg 2 Entities. My Model is named 2, because I already have a Singleton named Model for some managing.
So from that, I have Model2.h and Model2.m.
My Problem : The first time, my Model2 is initialise, I put in some default data, then I commit. Works good, it says that my Model2 has been saved correctly. After what I am reading my data, data are shown from the database.. so it is successfuly in the database. BUT.. when closing and killing my app, my app seems to have lost all data.. and start over again with creating default data cause it is empty...
TIP: I'm thinking my problem could be about the initialize.. whithin those lines:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL * storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: #"ProjectXYZ.db"]];
Since ProjectXYZ.db doesn't exist, it should create it?.. that's the part where I'm getting lost.. but it seems to worked on another project I've worked on... :S
Here's my Model2.h
#import <CoreData/CoreData.h>
#import "Photos_Trophies.h"
#import "Trophies.h"
#interface Model2 : NSObject
// High-level methods.
+ (void) commit;
...
// Object Retrieval
+ (NSArray*) trophies;
...
// Object Creation
+ (id) trophiesWithTitle:(NSString *)title;
#end
And my Model2.m
#import "Model2.h"
#import <UIKit/UIKit.h>
static NSManagedObjectContext * ctx;
#implementation Model2
+ (void) initialize {
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL * storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: #"ProjectXYZ.db"]];
NSError * error = nil;
ctx = [[NSManagedObjectContext alloc] init];
ctx.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
ctx.undoManager = [[NSUndoManager alloc] init];
if (![ctx.persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(#"%#", error);
}
//TEMPORARY... these are default trophies for example
if ([[Model2 trophies] count] == 0) {
[self trophiesWithTitle:#"Saved Trophy Test 1"];
[self trophiesWithTitle:#"Saved Trophy Test 2"];
[self trophiesWithTitle:#"Saved Trophy Test 3"];
[self commit];
}
}
+ (void) commit {
NSError* error = nil;
if (![ctx save:&error]) {
NSLog(#"ERREUR DANS COMMIT: %#", error.localizedDescription);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors)
NSLog(#"DetailedError: %#", [detailedError userInfo]);
} else
NSLog(#"%#", [error userInfo]);
} else
NSLog(#"Model2 SAVED");
}
+ (NSArray*) trophies {
NSFetchRequest* req = [[NSFetchRequest alloc] init];
req.entity = [NSEntityDescription entityForName:#"Trophies" inManagedObjectContext:ctx];
req.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"trophies_title" ascending:YES]];
NSError* error = nil;
NSArray* objects = [ctx executeFetchRequest:req error:&error];
return objects;
}
+ (id) trophiesWithTitle:(NSString *)title {
Trophies * trophies = [NSEntityDescription insertNewObjectForEntityForName:#"Trophies" inManagedObjectContext:ctx];
trophies.trophies_title = title;
return trophies;
}
Your problem is this: NSInMemoryStoreType You need to use a persistent backing store.
Do something like this:
NSURL *storeUrl = [NSURL fileURLWithPath:[self persistentStorePath]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
NSAssert(error == nil, #"error creating persistentStoreCoordinator");
}

Illegal attempt to establish a relationship between objects in different contexts

I have two managedobjectcontext
I fetch object from managedobjectcontext1 and want two save it in the second object context while try to do that I get the error: "Illegal attempt to establish a relationship between objects in different contexts". The code I use
NSError * error;
NSPersistentStoreCoordinator *persistentStoreCoordinator_OLD;
NSPersistentStoreCoordinator *persistentStoreCoordinator_NEW;
NSManagedObjectModel *managedObjectModel_OLD;
NSManagedObjectModel *managedObjectModel_NEW;
NSManagedObjectContext *managedObjectContext_OLD;
NSManagedObjectContext *managedObjectContext_NEW;
NSURL *storeUrl_OLD = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: #"CoreDataTutorialPart4.sqlite"]]; // CoreDataTutorialPart4.sqlite
NSURL *storeUrl_NEW = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: #"Newpaxera.sqlite"]]; // CoreDataTutorialPart4.sqlite
// ADJUST THE MODEL
managedObjectModel_OLD = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
managedObjectModel_NEW = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
persistentStoreCoordinator_OLD = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_OLD];
if (![persistentStoreCoordinator_OLD addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl_OLD options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
else {
NSLog(#"SUCCESS");
}
persistentStoreCoordinator_NEW = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_NEW];
if (![persistentStoreCoordinator_NEW addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl_NEW options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
}
else {
NSLog(#"SUCCESS");
}
managedObjectContext_OLD = [[NSManagedObjectContext alloc] init];
[managedObjectContext_OLD setPersistentStoreCoordinator: persistentStoreCoordinator_OLD];
managedObjectContext_NEW = [[NSManagedObjectContext alloc] init];
[managedObjectContext_NEW setPersistentStoreCoordinator: persistentStoreCoordinator_NEW];
NSFetchRequest *fetchRequest_Study = [[NSFetchRequest alloc] init];
NSEntityDescription *entity_Study = [NSEntityDescription
entityForName:#"Studies" inManagedObjectContext:managedObjectContext_OLD];
[fetchRequest_Study setEntity:entity_Study];
NSMutableArray *StudiesList = [managedObjectContext_OLD executeFetchRequest:fetchRequest_Study error:&error];
for(int i =0 ; i < [StudiesList count] ; i++){
Studies *study = [StudiesList objectAtIndex: i ];
Studies *study_NEW = (Studies *)[NSEntityDescription insertNewObjectForEntityForName:#"Studies" inManagedObjectContext:managedObjectContext_NEW];
study_NEW.SudyID = study.SudyID;
study_NEW.StudyDate=study.StudyDate;
study_NEW.ModalityName=study.ModalityName;
study_NEW.Studiesstudent = study.Studiesstudent ; // raise sigapart error here
Studiesstudent is an object of another entity class
Any suggestions for how to resolve this? Xcode does not give an error in the other numerical or string data.
You cannot transfer managed objects between context, especially when they the context are initialized to other persistent stores. You need to clone the managed objects in the new context i.e. create new objects with the same attributes as the old.
Are you sure you don't need to migrate the persistent stores? It looks like you are trying to update an existing store to a new model. That is what migration is for.

How to refresh a TableView when data is inserted in the AppDelegate?

To pre-populate my Database for a small Iphone-App I do a check in the persistentStoreCoordinator Method in the AppDelegate if the sqlite-file is already there or not. If not I add some data into a table after the db is created. But the data should not directly shown in the RootViewController but in another TableView which can be accessed by pressing a Option in the RootViewController.
The Problem is that the data are saved but are not shown in the TableView. But if terminate and restart the app the data are there.
I have passed the managedObjectContext from my appDelegate to the the RootViewController and from there to the TableViewController. So the managedObjectContext is the same everywhere.
Here is the Code where I pre-populate the DB (in this example just one sample row):
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"TestDBApp.sqlite"];
NSString *storePath = [storeURL relativePath];
BOOL noDb = false;
NSFileManager *fileManager = [NSFileManager defaultManager];
if( ![fileManager fileExistsAtPath:storePath]) {
noDb = true;
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
if( noDb) {
noDb = false;
[self populateDB];
}
return persistentStoreCoordinator_;
}
- (void) populateDB {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Kunde" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"oxid" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Root"];
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self insertNewKunde:aFetchedResultsController];
[self saveContext];
[aFetchedResultsController release];
[sortDescriptors release];
[sortDescriptor release];
[fetchRequest release];
}
- (void)insertNewKunde:(NSFetchedResultsController *)fetchedResultsController {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
[newManagedObject setValue:[[NSDate date] description] forKey:#"oxid"];
[newManagedObject setValue:#"Max" forKey:#"fname"];
[newManagedObject setValue:#"Mustermann" forKey:#"lname"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
The managedObjectContext is passed from the AppDelegate to the RootViewController as:
- (void)awakeFromNib {
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
}
and to the other TableViewController:
- (void)awakeFromNib {
kundenVC = [[KundenViewController alloc] initWithManagedObjectContext:self.managedObjectContext];
}
Can anyone help me or give a hint?
I also tried to reload the tableView through the viewWillAppear-Method. But no effect.
It is very difficult to guess what is happening but my hunch is that you are not calling self.populateDB until the RootViewController is actually created. This means the RootViewController does not see that data because it is not created.
I could be wrong but I guess that your last line in delegate's awakeFromNib method is eventually calling populateDB through self.managedObjectContext -> persistentStoreCoordinator -> self.populateDB.
Please try to add a line right at the top of awakeFromNib looking like this:
(void)awakeFromNib {
NSManagedObjectContext *context = self.managedObjectContext;
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = context;}
This way I hope the your problem is solved because self.managedObjectContext will call the getter (- (NSManagedObjectContext)managedObjectContext;) instead of the field. Inside there (if you use the generated code) there is a call to the getter of ** persistentStoreCoordinator** which eventually calls you populateDB method.
In case that does not work go ahead and set a breakpoint inside your populateDB. When the debugger stops you should see at the method call stack where you populateDB is actually called from and that might help to figure out your problem.
-Andy
Well, the important part you left out to answer your question. What is [self saveContext] and how are you displaying the records in your table view.
Because your data is saved I assume that the code given here works. The only guess I can make is to use reset on the NSManagedObjectContext and then refetch.
BTW I like the "insertNewKunde" which seems to be the new German grammar ;-)