objective c - Create NSManagedObject (big size). Memory warning and the application crashes -
i'm new ios developer. hope help. want able create many nsmanagedobjects. size of fields of 1 nsmanagedobject 5mb. couldn't save such big amount of memory in iphone memory. , want save in database. when save nsmanagedobject, it's still in memory, because when save 20 objects, memory warning , application crashes. here 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 error"); } nsmanagedobject *object; if ([objects count] > 0) { // edit item object = [objects objectatindex:0]; } else { // if object doesn't exist, find max id 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 5mb nsmutabledata *objnsdata = [[databasemanager shareddatabasemanager] encrypteddatafromimage:bigimage]; [object setvalue:objnsdata forkey:@"big"]; [context save:nil]; } when try call [self saveitem] 20 times , app crashes memory warning. when commented
[object setvalue:objnsdata forkey:@"big"]; everything ok.
i tried add code @autoreleasepool , didn't help.
i know, now, when save data database, it's still in iphone memory. how release memory? when set of managed objects, not in memory (i can easyly 100 object, each of them has 5mb fields)
storing images (or big binary values) in core data not option. apple recommends against it. there rules that. check other question/answer.
if want objects out of memory, need remove references have them. if executing "saveitem" in tight loop, should wrap every loop execution in autorelease pool, not loop itself.
from apple docs, may problem too:
managed objects have pending changes (insertions, deletions, or updates) retained context until context sent save:, reset , rollback, or dealloc message, or appropriate number of undos undo change. undo manager associated context retains changed managed objects. default, context's undo manager keeps unlimited undo/redo stack. limit application's memory footprint, should make sure scrub (using removeallactions) context's undo stack , when appropriate. unless retain context's undo manager, deallocated context.
if not intend use core data's undo functionality, can reduce application's resource requirements setting context’s undo manager nil. may beneficial background worker threads, large import or batch operations.
Comments
Post a Comment