I want to deal with a data attribute, named originalImage in every Media entity in the store.
The problem is that despite the autoreleasing, memory builds up every time it's accessed via valueForKey, and eventually the app crashes. Or perhaps it's loading large individual NSData items which is the problem, but Instruments shows it to be a steadily inclining graph of memory usage, until it eventually gives me a memory warning and then crashes.
I haven't started on the rest of the code for this yet, so i'm not performing some hidden task that i'm not showing you.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Media"
inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
[request setFetchBatchSize:10];
NSArray *mediaItems = [[self managedObjectContext] executeFetchRequest:request error:nil];
for (NSManagedObject *media in mediaItems) {
#autoreleasepool {
[media valueForKey:#"originalImage"];
}
}
EDIT: Today it seems that even just mentioning the NSManagedObject media is enough to cause this media leak. So even without the valueForKey line, i have a leak. I've tried this:
while (i < count) {
#autoreleasepool {
NSManagedObject *media = [mediaItems objectAtIndex:i];
[[self managedObjectContext] refreshObject:media mergeChanges:NO];
NSLog(#"i: %i", i);
i++;
}
}
This also didn't work, and crashed at the same point.
Did you try something like:
for(int i=0;i<[mediaItems count]; i++) {
#autoreleasepool {
NSManagedObject *media = [mediaItems objectAtIndex:i];
[media valueForKey:#"originalImage"];
}
}
Maybe this way "media" gets released properly at every loop.
(Sorry, I put this on a comment. I'm new on SO and not enough rep.)
Related
I'm using an NSFetchRequest to get all core data entities in a context, and I'm moving them to another context.
This works fine doing it with just 1 entity. I can iterate through results very quickly. The entity has a relationship with another entity however, so I need to run a second NSFetchRequest to get the entities which are joined by this relationship. It's the second NSFetchRequest which is causing the crash. The crash just makes the app quit to the homescreen and no errors appear in the xcode log. I've commented out my code to figure out that this is the problem, and have got it down to this:
NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
NSManagedObjectContext *oldContext = [self version1ManagedObjectContext];
TICDSSynchronizedManagedObjectContext *newContext = [self version1_1ManagedObjectContext];
NSFetchRequest *oldFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *oldEntryEntity = [NSEntityDescription entityForName:#"Entry"
inManagedObjectContext:oldContext];
[oldFetchRequest setEntity:oldEntryEntity];
[oldFetchRequest setFetchBatchSize:10];
NSArray *entrys = [oldContext executeFetchRequest:oldFetchRequest error:&error];
int totalEntries = [oldContext countForFetchRequest:oldFetchRequest error:nil];
NSLog(#"total entries: %i", totalEntries);
int i = 0;
while (i < totalEntries) {
#autoreleasepool {
Entry *entry = [entrys objectAtIndex:i];
Entry *newEntry = [NSEntityDescription
insertNewObjectForEntityForName:#"Entry"
inManagedObjectContext:newContext];
//Taking out this fetch request means it functions fine
NSFetchRequest *mediaRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *mediaEntity = [NSEntityDescription
entityForName:#"Media"
inManagedObjectContext:oldContext];
[mediaRequest setEntity:mediaEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(entry == %#)", entry];
[mediaRequest setPredicate:predicate];
NSArray *mediaItems = [oldContext executeFetchRequest:mediaRequest error:&error];
int totalMediaItems = [oldContext countForFetchRequest:mediaRequest error:nil];
NSLog(#"total media items: %i", totalMediaItems);
int i2 = 0;
while (i2 < totalMediaItems) {
#autoreleasepool {
Media *newMedia = [NSEntityDescription
insertNewObjectForEntityForName:#"Media"
inManagedObjectContext:newContext];
i2++;
}
}
[newContext save:&error];
i++;
}
}
Any ideas why this might be crashing my app?
In answer to any questions about what I'm trying to do - I'm trying to migrate data between 2 versions. Standard data migration, using mapping, does not work with large data, such as NSData.
I believe that there are few more code lines that you don't expose here. The ones where you copy the data from the old entities to the new ones...
Maybe the problem is there.
Anyway, I would add NSLog between every 2 lines of code and see which is the last one that is printed...
I think that something is autoreleased in the inner loop and you try to use it in the next iteration.
I suggest that you edit the scheme that you are using to build and enable everything for the diagnostics tab. This will probably spit out whatever might be causing the issue.
I want to insert 200 5Mb records in my Core Database. But when I save the NSManagedObject, the memory wasn't released (autoreleased pool didn't help), and after inserting 30 records I got the memory warning and the application crashed. Here is my code
- (void)SaveItem
{
NSString *entityName = kEntityName;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = appDelegate.managedObjectContext;
NSEntityDescription *entityDesctiption = [NSEntityDescription
entityForName: entityName
inManagedObjectContext:context];
// check if town exists
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"id == %d", self.imageID];
NSFetchRequest *requestToCheckExistense = [[NSFetchRequest alloc] init];
[requestToCheckExistense setEntity:entityDesctiption];
[requestToCheckExistense setPredicate:predicate];
NSArray *objects = [context executeFetchRequest:requestToCheckExistense error:nil];
[requestToCheckExistense release];
if (objects == nil)
{
NSLog(#"there was an error");
}
NSManagedObject *object;
if ([objects count] > 0)
{
// edit item
object = [objects objectAtIndex:0];
}
else
{
// if object doesn't exist, find max id to imlement autoincrement
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesctiption];
request.propertiesToFetch = [NSArray arrayWithObjects: #"id", nil];
NSArray *allobjects = [context executeFetchRequest:request error:nil];
[request release];
NSInteger newID = 1;
if ([allobjects count] > 0)
{
NSNumber *maxID = [allobjects valueForKeyPath:#"#max.id"];
newID = [maxID intValue] + 1;
}
// write item
object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[object setValue:[NSNumber numberWithInt:newID] forKey:#"id"];
self.imageID = newID;
}
// fill NSManagedObject
// size of objNSData is about 5MB
NSMutableData *objNSData = [[DatabaseManager sharedDatabaseManager] encryptedDataFromImage:bigImage];
[object setValue:objNSData forKey:#"big"];
[context save:nil];
}
When I commented
[object setValue:objNSData forKey:#"big"];
everything was OK.
I tried to add the code into #autoreleasepool , but that didn't help.
I know, that now, when I save data to database, it's still in iPhone RAM. How to release it from this memory? When I get a set of Managed Objects from the database, they are not in the RAM (I can easyly get 100 object, each of them has 5Mb fields)
object =(tblEntity *) [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
try to type cast the object,this may solve the problem
I've solved the issue.
after call of [self SaveItem];
I used
[context save];
[context reset];
[context save];
all the NSManagedObjects from the context will be released.
After that operation I can add as many big objects as I want
Because you don't own an NSManagedObject when you create it, it may be retained by the core data stack even after releasing it (when using an autoreleasepool contained inside the loop).
This may help:
Set the undo manager of your managedobjectContext to nil:
[context setUndoManager:nil];
Be sure that no properties of that object are retained anywhere, because then the managed object will not be released on time inside your loop.
Be sure to add an autorelease pool inside every loop execution, not wrapping all the loop itself, similar to:
for(i;i<n;i++) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[obj saveItem];
[pool drain];
}
If that object belongs to a hierarchy of NSManagedObjects, then you need to release the owner of this object too, for this one to be deallocated from memory.
You can check apple's documentation about memory management in CoreData.
Warning: big objects (> 1MB) are not recommended by Apple to be stored inside CoreData (Check this other question/answer.)
Okay, I'm having a problem saving after I've deleted all the objects I have stored in CoreData. I have no problem with saving if I don't delete anything, but as soon as I ask it to delete all the objects (everything deletes with no errors or problems), and then try saving again, it crashes and just gives me a program received signal: SIGABRT. Here's my code.
- (void)deleteStoredData
{
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:MOVIE_LIST inManagedObjectContext:managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Fetch the records and handle an error
NSError *error;
NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error];
if (count) {
for (int i = 0; i < count - 1; i++) {
NSManagedObject *eventToDelete = [self.listOfMovies objectAtIndex:i];
[managedObjectContext deleteObject:eventToDelete];
}
}
[request release];
}
I thought it might be me comparing self.listOfMovies to the objects stored, so I did a fresh fetch, copied it to a temp NSMutableArray, then replaced self.listOfMovies with temp. But no changes, still crashes. Did I some how delete the entire record and it no longer exists?
What I want to do is load everything onto the app, then delete all the objects in CoreData, so that when the app closes (or terminates) it saves all the new data in the records. Am I doing this correctly, or is there a much easier way to do this? Oh yea, and I only have one entity that holds 5 NSStrings, so nothing to complicated.
Thanks in advance everyone.
Possibly, when you call the save method, there might be some mixup with some variable such as the managedObjectContext.
Did you try saving right after the deletion (i.e. in your deleteStoredData method above)?
BTW, I would also go with Christopher's code;-).
Deleting managed objects with a for loop like that is error prone and probably corrupting your managedObjectContext. Try the following:
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:MOVIE_LIST inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (NSManagedObject * event in result) {
[context deleteObject:event];
}
Since you are already have all of managedObject in your array, you do NOT need to do another fetch, just delete them with the code below should be OK.
for (NSUInteger i = 0; i < [self.listOfMovies count] - 1; i++) {
NSManagedObject *eventToDelete = [self.listOfMovies objectAtIndex:i];
[managedObjectContext deleteObject:eventToDelete];
}
If it still have problem, would you please tell me how many managedObjectContext do you have in your App? Are you deleting or saving it in a background thread?
In addition, would you please also post the crash log and the information by type "bt -> enter key" in console after your App crashed?
I have the following code, and I was wondering if theres any way to make this faster. Basically my app downloads some JSON (about 4000 records) from the net, and updates or creates my managed objects based on the data. At the moment it's quite slow, and I can see why, but I'm new to core data so I was wondering if there's anything I can do to make it faster?
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Company" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSMutableArray *coreDataArray = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
[request release];
for (NSDictionary *dict in arr) {
NSArray *filtered = [coreDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(code == %#)", [dict objectForKey:#"Code"]]];
//NSLog(#"COREDATA ARRAY: %d FILTERED ARRAY: %d CODE: %# COREDATA FIRST CODE: %#", [coreDataArray count], [filtered count], [dict objectForKey:#"Code"], [[coreDataArray objectAtIndex:0] code]);
if ([filtered count] > 0) {
Company *c = [filtered objectAtIndex:0];
if ([dict objectForKey:#"Defunct"]) {
NSLog(#"DELETED DEFUNCT COMPANY");
[managedObjectContext deleteObject:c];
} else {
[c populateWithJSONDictionary:dict];
}
} else {
Company *c = (Company *)[NSEntityDescription insertNewObjectForEntityForName:#"Company" inManagedObjectContext:managedObjectContext];
[c populateWithJSONDictionary:dict];
}
float percent = (float)[arr indexOfObject:dict]/[arr count];
[self performSelectorInBackground:#selector(updateProgressView:) withObject:[NSString stringWithFormat:#"%f",percent]];
}
[coreDataArray release];
Many thanks for any help you can give.
You should check out the Core Data Programming Guide: Performance section
It has some specific advice for data import performance.
In case Apple moves the documentation again, here is a good search query on Google site:developer.apple.com core data import performance
I desperately need help with a memory leak in my iPhone app. The app is ready to submit to the app store, is stable, has no memory leaks at all in iPhone simulator or Clang ... but seems riddled with them on my iPod Touch.
They all seem to stem from managedObjectModel when I'm trying to retrieve data from Core Data.
The Core Data code in my app was automatically created by Xcode a while back, I've noticed that the code has since changed when you get xcode to generate it ... I've tried with the old and new but it makes no difference.
If I comment out the following code, the problem goes away ... can anyway see what's wrong with it? I've spent 9 hours on this so far and just can't figure it out!
NSString *entityForName = [[NSString alloc] initWithString:#"OfflineSettings"];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityForName inManagedObjectContext:[self managedObjectContext]];
[request setEntity:entity];
[entityForName release];
NSSortDescriptor *sortById = [[NSSortDescriptor alloc] initWithKey:#"Id" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortById]];
[sortById release];
NSError *error;
NSMutableArray *mutableFetchResults = [[[self managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
NSLog(#"Error fetching");
}
int intId = -1;
if ([mutableFetchResults count] == 0) {
TTDERROR(#"No id has been saved to offline settings");
} else {
OfflineSettings *offlineSettings = (OfflineSettings *)[mutableFetchResults objectAtIndex:0];
intId = [offlineSettings.Id intValue];
}
[mutableFetchResults release];
[request release];
The leak specifically seems to be on this line:
NSMutableArray *mutableFetchResults = [[[self managedObjectContext] executeFetchRequest:request error:&error] mutableCopy];
.. and the code for [self managedObjectContext] is as follows in case it helps ..
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
I'm really at a loss, so I would be so grateful for some help!
Steven
You don't need the mutable copy. executeFetchRequest: returns an autoreleased static array and you're not mutating the array. (I keep seeing this. Must be in an example somewhere.) Likewise, creating the entityForName NSString is pointless. Just put the string literal in the entityForName: to eliminate another possible source of error.
Niether of these are the likely source of the leak but you should remove them anyway.
As a rule of thumb, if you have troubles on device but not simulator or on one hardware but not others, then the problem is in a library/framework that is not properly compiled for the hardware where the error occurs. There really isn't any type of coder error that leaks in one environment but not others. When we make a mistake, it's universal.
It's also possible for resources such as images and sounds to behave differently because different devices use different graphics and audio hardware. That, however, is rather rare.
If you run the code through Instruments it should tell you exactly what object is leaking.