4

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?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • You shouldn't have to refresh the managedObjectContext for those situations where you are not getting up to date data. You might want to check if you are saving to your context or pushing changes from a child context to its parent context and saving the parent context. – timthetoolman Jun 12 '12 at 23:10
  • i don't understand what you mean... – Piero Jun 12 '12 at 23:47
  • you don't have to refresh your managedObjectContext. an error in your logic elsewhere is preventing your views from refreshing data. – timthetoolman Jun 13 '12 at 00:59
  • Unless you are using a tableview and fetchedResultsController, you are responsible for keeping your views "synced" with your model when an update occurs. So for instance if you have a view displaying your coredata object, then you open another view to edit it, when you go back to the displaying view, you need to make sure that view is "refreshed" with the updated coredata model. You probably want to do that in your `viewWillAppear:` method. – Rog Jun 13 '12 at 05:47
  • So you want say me that i have to use the fetchedresultscontrellee? – Piero Jun 13 '12 at 07:55

2 Answers2

1

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

Bad Boy
  • 628
  • 2
  • 5
  • 23
  • ru calling reloaddata on ur table View ?...once u save the Context? – Bad Boy Jun 13 '12 at 08:05
  • in the view will appear i call this method - (NSArray *)sortInformation{ i write above, so i think is refreshed, but some time it's note refreshd, so maybe i have to delete the if (managedObjectContext == nil) { ? – Piero Jun 13 '12 at 09:00
  • call [tableView reloadData]; after updating ur data or [managedObjectContext save:&error]; try this – Bad Boy Jun 13 '12 at 09:13
  • i don't have the fethed result controller, i should have this? – Piero Jun 13 '12 at 09:17
0

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;
}
DLende
  • 5,162
  • 1
  • 17
  • 25
  • yes the app delegate managedObjectContext it should return like you say, so i try to do this: managedObjectContext = [(AppDelegate *) [[UIApplication sharedApplication] delegate] managedObjectContext]; but in this way i don't use too much memory? – Piero Jun 12 '12 at 22:26
  • i don't have a database class, what is the database class? – Piero Jun 12 '12 at 23:48