I have a a main NSManagedObjectContext
used in a few UIViewControllers to display the data (which is a UITableView
with a list of Department
)
3 entities one Department
with a to-one to Boss
with a to-many to Employee
(In this case the employee
have an NSData
(which is an image) attribute with allow external storage
).
Since I'm importing images in batches I'm doing it in a background thread which has its own NSManagedObjectContext
.
The importing consists in creating the Boss
entity and all the Employee
and setting up the relationships.
Now my issue is :
- if I use a
child context
of themain context
for importing and save, then all the images stay in memory even though both context don't have changes. - if I use a
context with no relation to the main context
the image aren't staying in memory but the new data isn't showed in theUIViewController
(obviously since themain context
isn't notified of the changes done by thebackground context
)
So I would like to still have the changes appear without having the images in memory (meaning I would like the Department
to know that it has a Boss
relationship but without having the images in memory). In short I would like them to be turned into fault as soon as the context
is saved.
EDIT : I think the problem is when I save the child context
, it merges with the main context
and from there the newly inserted images stay in memory :/ and I have no idea how to release them (and no they're not auto released even with memory warning...)
EDIT 2 : I think I fixed it, here's what I did :
- I used a
child context
tied to themain context
and I listened to all theNSManagedObjectContextDidSaveNotification
and for all theinserted
updated
I callrefreshObject:mergeChanges:
on it to turn it into fault.
I registered for all the notifications from every context.
-(void)contextDidSave:(NSNotification*)saveNotification {
NSManagedObjectContext *defaultContext = saveNotification.object;
NSArray *insertedObjects = [saveNotification.userInfo valueForKey:@"inserted"];
if (insertedObject) {
NSLog(@"INSERTED : %@", insertedObjects);
for (NSManagedObject *object in insertedObjects) {
[defaultContext refreshObject:object mergeChanges:NO];
}
}
NSArray *updatedObjects = [saveNotification.userInfo valueForKey:@"updated"];
if (insertedObject) {
NSLog(@"UPDATED : %@", updatedObjects);
for (NSManagedObject *object in updatedObjects) {
[defaultContext refreshObject:object mergeChanges:NO];
}
}
}