ios - managed object value is randomly null (Sample project included) -
after saving data (10 records) entity, processing fetch request data again:
//saving data - (void)parser:(nsxmlparser *)parser didendelement:(nsstring *)elementname namespaceuri:(nsstring *)namespaceuri qualifiedname:(nsstring *)qname{ //save coredata song = [nsentitydescription insertnewobjectforentityforname:@"song" inmanagedobjectcontext:context]; [song setvalue:title forkey:@"title"]; [song setvalue:songlink forkey:@"songweblink"]; nslog(@"song link : %@",songlink);//never null [song setvalue:albumlink forkey:@"albumimagelink"]; nserror *error = nil; if (![context save:&error]) { nslog(@"whoops, couldn't save: %@", [error localizeddescription]); }else{ nslog(@"record saved correctly"); } }
saving above working fine , debugged data before being saved context ensure none of attributes null
.
the problem songweblink
attribute, gets null when try ot below:
- (void)parserdidenddocument:(nsxmlparser *)parser{ nsentitydescription *songentity=[nsentitydescription entityforname:@"song" inmanagedobjectcontext:context]; nsfetchrequest *fetch=[[nsfetchrequest alloc] init]; [fetch setentity:songentity]; nserror *fetcherror; nsarray *fetchedsongs=[context executefetchrequest:fetch error:&fetcherror]; nsmutablearray *songs = [[nsmutablearray alloc] init]; (nsmanagedobject *songobject in fetchedsongs) { //here issue: loop go through 10 iterations, songweblink randomly null, it's fourth iteration, 8th, 5th. nslog(@"song web link: %@",[songobject valueforkey:@"songweblink"]);//albumimagelink , title attributes fine } }
the issue when nslog
songweblink
, gets null, once 4th iteration, 6th, 2nd etc. it's randomly assigning null attribute when fetching. when saving data, make sure there no null
value songweblink
. bet on else cause of issue.
any thoughts?
edit
here how moc initialized:
appdelegate.m:
- (nsmanagedobjectcontext *)managedobjectcontext { if (_managedobjectcontext != nil) { return _managedobjectcontext; } nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator]; if (coordinator != nil) { _managedobjectcontext = [[nsmanagedobjectcontext alloc] init];//i tried initwithconcurrencytype:nsmainqueueconcurrencytype [_managedobjectcontext setpersistentstorecoordinator:coordinator]; } return _managedobjectcontext; }
getting moc object use in different class :
appdelegate *appdelegate = (appdelegate*)[[uiapplication sharedapplication]delegate]; context = [appdelegate managedobjectcontext];
sample project: if think need on app project, made simplified version on reproduced bug, can download here.
give try:
include in fetch request
[fetch setincludespropertyvalues:yes];
iterate through returned objects this:
for (song *songobject in fetchedsongs) { //here issue: loop go through 10 iterations, songweblink randomly null, it's fourth iteration, 8th, 5th. nslog(@"song web link: %@",songobject.songweblink);//albumimagelink , title attributes fine }
i'm not 100% sure fix in finding problem if doesn't.
Comments
Post a Comment