1

I would like to remove my sql lite file and set up the persistance store again.

//Explicitly write Core Data accessors

- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {

    return managedObjectContext;

}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

if (coordinator != nil) {

    managedObjectContext = [[NSManagedObjectContext alloc] init];

    [managedObjectContext setPersistentStoreCoordinator: coordinator];
}

    return managedObjectContext;
}

- (void) setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext{}


- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil] ;

    return managedObjectModel;
}

-(void) setManagedObjectModel:(NSManagedObjectModel *)managedObjectModel{}


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]    stringByAppendingPathComponent: @"Port.sqlite"]];    
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {

        NSLog(@"Could not create store ....  %@", error );
        /*Error for store creation should be handled in here*/
    }

return persistentStoreCoordinator;
}

I am trying to reset my scene like this

- (void)reset {
    // Release CoreData chain
    self.managedObjectContext = nil;
    self.managedObjectModel = nil;
    self.persistentStoreCoordinator = nil;

    // Delete the sqlite file
    NSError *error = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]    stringByAppendingPathComponent: @"Port.sqlite"]];  

    if ([fileManager fileExistsAtPath:storeUrl.path]){
        [fileManager removeItemAtURL:storeUrl error:&error];
    }
    self.managedObjectContext = [self managedObjectContext];
    self.managedObjectModel = [self managedObjectModel];
    self.persistentStoreCoordinator = [self persistentStoreCoordinator];

    // handle error...
}

I am getting an error while saving:

+ (BOOL)saveAll {
    // [self createStorage];
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = [(WSSMobileAppsAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    if (![managedObjectContext save:&error]) {
        NSLog(@"Error while saving %@", error);
        return FALSE;
    }
    return TRUE;
}

The error:

Error while saving Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x7195ac0 {NSAffectedStoresErrorKey=(
    "<NSSQLCore: 0x714eee0> (URL: file://localhost/Users/.../Library/Application%20Support/iPhone%20Simulator/5.0/Applications/CEDB9019-1D64-4968-9BE7-57E1493B96EC/Documents/Port.sqlite)"
), NSUnderlyingError=0x7195a50 "The operation couldn’t be completed. (Cocoa error 4.)", NSFilePath=/Users/.../Library/Application Support/iPhone Simulator/5.0/Applications/CEDB9019-1D64-4968-9BE7-57E1493B96EC/Documents/Port.sqlite}

I only get the error if I run my reset function. I thought that setting:

self.managedObjectContext = nil;
self.managedObjectModel = nil;
self.persistentStoreCoordinator = nil;

...would solve the problem. Then everything would be recreated. Please help.


Thank you very much for your answers. Now there is no error but nothing gets stored if I run my reset. I do a fetch right after saving

if (![managedObjectContext save:&error]) {
    NSLog(@"Error while saving %@", error);
    return FALSE;
}
CoreDataPortService *c = [[CoreDataPortService alloc] init];
NSLog(@"Saved all.... got number of ports ... %d", [[c getPorts] count]);

I if don't run my reset method everything works as expected. What can be wrong?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
pethel
  • 5,397
  • 12
  • 55
  • 86
  • I thought the point was you wanted to delete all your data. No? If you delete the file, and delete the managedObjectContext in memory, the data is gone. If you're trying to delete the file and recreate it from the contents of memory, then you would need a different approach. I can't really imagine why you would want to do that. The persistent store will keep in sync with what you have in the managed object context in normal cases. – morningstar Sep 18 '11 at 00:26
  • I would like to delete the data. Is it possible to also delete the file. Thats because sometimes I change entities. – pethel Sep 20 '11 at 09:36
  • You said, "nothing gets stored if I run my reset". It sounds like you want nothing stored, i.e. everything deleted. Do you mean you run reset, then add objects, then save, and there are no objects after that? – morningstar Sep 20 '11 at 18:48
  • Sorry for confusing you. When I run my reset I want to delete everything. After I have deleted everything I want to parse my xml again and store the state of my objects. So storing the state of my XML works i I dont run my reset before. So 1. Reset all objects (delete XML) 2. Parse new XML and store that. Step 2 is now my problem. Step 2 works If I dont run step 1 before. – pethel Sep 21 '11 at 05:54

1 Answers1

0

You wrote a setter for your managedObjectContext, but it does nothing:

- (void) setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext{}

When you do self.managedObjectContext = nil, all it does is call [self setManagedObjectContext:nil]. That does nothing. The instance variable managedObjectContext keeps the same value, and when you request it again with the getter, you still have the managedObjectContext that was associated with the deleted file. You need:

- (void) setManagedObjectContext:(NSManagedObjectContext *)_managedObjectContext {
    [managedObjectContext release];
    managedObjectContext = _managedObjectContext;
    [managedObjectContext retain];
}

And that's also assuming your managedObjectContext property is declared with the nonatomic modifier. If it's declared atomic, you will also need code to lock. Write the setters for managedObjectModel and persistentStoreCoordinator in a similar way.

@synthesize usually generates setters like that for you, but since you want to write your own getters, you have to write your own setters too. There might be a way to have @synthesize make only the setter for your property instead, but I can't remember.

Answer to follow up question. Maybe you are doing something like this:

NSManagedObjectContext *moc = self.managedObjectContext;
[self reset];
[self repopulateData:xmlFile];
NSError *error = nil;
if (![moc save:&error])
    [self logError:error];

If that's the case then you would be calling save on the old managed object context. Not sure what the outcome of that would be. Instead change the next-to-last line to

if (![self.managedObjectContext save:&error])

to make sure you are using the current managed object context.

Or similarly you might be trying to read from the old managed object context. Check the address of any managed object context pointers you are using and make sure they are pointing to the same thing as the new managed object context you created in reset.

If that's not the problem, I have no idea. You will have to try to gather more information as to why it's not working. For example, does the file exist at all? Does its size increase when you add the data? Is the data written to the file, but can't be read?

morningstar
  • 8,952
  • 6
  • 31
  • 42
  • 1
    Minor quibble. It's incorrect that if you want custom getter you have to write custom setters as well (or vice versa.) If a needed method doesn't exist, the @sythesize creates it, if it exist, it uses that one. You can mix and match all you want. – TechZen Sep 16 '11 at 20:25
  • Ok, that's a better answer then. Remove `-(void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext {}` and add `@synthesize managedObjectContext`. – morningstar Sep 16 '11 at 22:09
  • @user87444 -- If this answer fixes your problem hit the check mark next to it so it shows as answered. – TechZen Sep 18 '11 at 15:07
  • Well the error are gone but something else has to be done to be able to save. – pethel Sep 22 '11 at 05:58