Piero
Piero

Reputation: 9273

refresh reference ManagedObjectContext

in my iOS app i have a core data, and i have notice that sometime, in a specific view, when i retrieve information from core data, are not always up to date, i'm explain well:

if i update some value in the core data, and then i go in in a specific view to view this information, that information are not up to date, now i show how i access my database:

.h

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

.m

@synthesize managedObjectContext;

- (NSArray *)sortInformation{

if (managedObjectContext == nil) {

    managedObjectContext = [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];

}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription 
                               entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

....

and then i display my information in a table view, all work perfectly, there is only this problem, that SOME TIME seems that the update i have done in another view is not read in this view, but if i close the app, and i close it from the background, and then i reopen it all works fine...so i have saved the update in the core data correctly, so i think the problem is in this view, maybe i have an old reference of the core data and not the update version, maybe the problem is this:

if (managedObjectContext == nil) {

    managedObjectContext = [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext];

}

that refresh only if the variable managedObjectContext is nil so only if the view is deallocated...so never, because is one of my root view controller in a UITabbarController, so my question is, how i can access to the core data to have always a update version of it?

Upvotes: 4

Views: 3209

Answers (2)

Bad Boy
Bad Boy

Reputation: 628

no need to refresh the context just call save method on managedObjectContext like [managedObjectContext save]; or if you are using more than one managed object context you should merge changes done by the context

Upvotes: 1

DLende
DLende

Reputation: 5242

On the implementation of the database class you can do like this

-(id) initWithContext: (NSManagedObjectContext *)managedObjContext {
    self = [super init];
    [self setManagedObjectContext:managedObjContext];
    return self;    
}

the managedObjContext is pass and set

On your app delegate when call the database class it should be something like this

database = [[Database alloc] initWithContext:self.managedObjectContext];

Then you are good to accessed the database like this

- (NSArray *)sortInformation {
           NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
           NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

           NSMutableArray *mutableFetchResults = [[[managedObjectContext_ executeFetchRequest:request error:&error] mutableCopy] autorelease];
           [request release];
           return mutableFetchResults;
}

Upvotes: 0

Related Questions