objective c - How can i parse very long JSon string in iOS without memory error? -
i new on ios,i have webservice , tring download 9000+ product info , have long json string,so got error message when products download,
malloc: * mmap(size=1978368) failed (error code=12) error: can't allocate region ** set breakpoint in malloc_error_break debug
i tried download below 1000 product info , working well. think, memory problem.
this code part of project.
-(void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname{ if (temp!=nil && ![temp isequal:@""]) { //basliklar arrayine ilgili tek haberin basligini aktariyoruz. //nslog(@"temp=%@",temp); nsdata *data = [nsjsonserialization jsonobjectwithdata:[temp datausingencoding:nsutf8stringencoding] options:0 error:nil]; urunadi=[data valueforkey:@"urunadi"]; urunno=[data valueforkey:@"urunno"]; koleksiyon=[data valueforkey:@"koleksiyon"]; kompozisyon=[data valueforkey:@"kompozisyon"]; grm=[data valueforkey:@"grm"]; urmid=[data valueforkey:@"urmid"]; en=[data valueforkey:@"en"]; islemtipi=[data valueforkey:@"islemtipi"]; dizim=[data valueforkey:@"dizim"]; kartelekodu=[data valueforkey:@"kartelekodu"]; siklik=[data valueforkey:@"siklik"]; euro=[data valueforkey:@"euro"]; dolar=[data valueforkey:@"dolar"]; sterlin=[data valueforkey:@"sterlin"]; tl=[data valueforkey:@"tl"]; [sharedmanager getnewdbconnection]; if(![temp isequaltostring:@"false"]){ sqlite3 *contactdb; nsarray *dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *docsdir = [dirpaths objectatindex:0]; nsstring *databasepath = [[nsstring alloc] initwithstring: [docsdir stringbyappendingpathcomponent: sql]]; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: databasepath ] == yes) { const char *dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &contactdb) == sqlite_ok) { nsstring *deletesql = [nsstring stringwithformat: @"delete tblkumasbilgi"]; const char *sql_stmt = [deletesql utf8string]; if(sqlite3_exec(contactdb, sql_stmt, null, null, null) == sqlite_ok) { // nslog (@"delete userdata succesful"); } else { nslog(@"delete userdata fail"); } //sqlite3_close(contactdb); } else { nslog(@"failed open database"); } } } if([temp isequaltostring:@"false"]){ [svprogresshud dismiss]; nsstring *warning = nslocalizedstring(@"warning",nil); nsstring *check=nslocalizedstring(@"check user info",nil); nsstring *okay=nslocalizedstring(@"ok", nil); uialertview *alertviewuser = [[uialertview alloc] initwithtitle:warning message:check delegate:self cancelbuttontitle:okay otherbuttontitles:nil, nil]; [alertviewuser show]; alertviewuser.tag=1; flag=no; } else{ sqlite3 *contactdb; nsarray *dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *docsdir = [dirpaths objectatindex:0]; nsstring *databasepath = [[nsstring alloc] initwithstring: [docsdir stringbyappendingpathcomponent: sql]]; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: databasepath ] == yes) { const char *dbpath = [databasepath utf8string]; if (sqlite3_open(dbpath, &contactdb) == sqlite_ok) { (int = 0; < [urunno count]; i++) { nsstring *insertsql = [nsstring stringwithformat: @"insert tblkumasbilgi (urunadi,urunno,koleksiyon,kompozisyon,grm,urmid,en,islemtipi,dizim,kartelekodu,siklik,euro,dolar,sterlin,tl) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')", [urunadi objectatindex:i], [urunno objectatindex:i], [koleksiyon objectatindex:i], [kompozisyon objectatindex:i], [grm objectatindex:i], [urmid objectatindex:i], [en objectatindex:i], [islemtipi objectatindex:i],[dizim objectatindex:i],[kartelekodu objectatindex:i], [siklik objectatindex:i],[euro objectatindex:i],[dolar objectatindex:i],[sterlin objectatindex:i], [tl objectatindex:i]]; //nslog(@"insert =%@",insertsql); const char *sql_stmt = [insertsql utf8string]; if(sqlite3_exec(contactdb, sql_stmt, null, null, null) == sqlite_ok) { //nslog (@"insert userdata succesful"); } else { nslog(@"insert userdata fail"); } } } else { nslog(@"failed open database"); } //sqlite3_close(contactdb); } [svprogresshud dismiss]; nsstring *warning = nslocalizedstring(@"warning", nil); nsstring *datasuccesful=nslocalizedstring(@"data download succesfully", nil); nsstring *okay=nslocalizedstring(@"ok", nil); uialertview *alertviewdownload = [[uialertview alloc] initwithtitle:warning message:datasuccesful delegate:self cancelbuttontitle:okay otherbuttontitles:nil, nil]; [alertviewdownload show]; alertviewdownload.tag=2; flag=no; } tempelement=@"";
} }
thank interest, , sorry bad english.
best regards.
either don't download @ once, or, if have to, download , save json disk. use json library gives parser (like this one) , parse json in parts.
Comments
Post a Comment