objective c - ios Core data bug with predicate -
i have strange behaviour core data.
i have entity card
, have nsnumber
property idobject
when want change info in card, try find similar card in coredata, work, , not. mean next:
nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"card"]; nspredicate *predicate = [nspredicate predicatewithformat:@"idobject == %@", objectid]; request.predicate = predicate; nserror *error; nsarray *objects = [self.backgroundmanagedobjectcontext executefetchrequest:request error:&error];
sometimes code return objects array, return nil. add next code after previous part of code:
request.predicate = nil; nsarray *cards = [self.backgroundmanagedobjectcontext executefetchrequest:request error:nil];
error nil, must fine. , cards
array has card
idobject
, try find. if try execute next code several times:
nslog(@"idobject -%@-", objectid); nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"card"]; nspredicate *predicate = [nspredicate predicatewithformat:@"idobject == %@", objectid]; request.predicate = predicate; nserror *error; nsarray *objects = [self.backgroundmanagedobjectcontext executefetchrequest:request error:&error]; nslog(@"objects count = %i", objects.count); request.predicate = nil; nsarray *cards = [self.backgroundmanagedobjectcontext executefetchrequest:request error:nil]; nslog(@"\ncards:"); (card *card in cards) { nslog(@"id -%@-", card.idobject); }
so have next results. in first time can see next:
2014-03-10 11:58:47.115[4397:6613] idobject -1- 2014-03-10 11:58:47.127[4397:6613] objects count = 1 2014-03-10 11:58:47.134[4397:6613] cards: 2014-03-10 11:58:47.145[4397:6613] id -5- 2014-03-10 11:58:47.146[4397:6613] id -4- 2014-03-10 11:58:47.148[4397:6613] id -1- 2014-03-10 11:58:47.148[4397:6613] id -2- 2014-03-10 11:58:47.149[4397:6613] id -6- 2014-03-10 11:58:47.150[4397:6613] id -8- 2014-03-10 11:58:47.151[4397:6613] id -7- 2014-03-10 11:58:47.152[4397:6613] id -3-
in second return me:
2014-03-10 11:59:42.752[4397:9e2b] idobject -1- 2014-03-10 11:59:42.758[4397:9e2b] objects count = 0 2014-03-10 11:59:42.761[4397:9e2b] cards: 2014-03-10 11:59:42.762[4397:9e2b] id -1- 2014-03-10 11:59:42.764[4397:9e2b] id -5- 2014-03-10 11:59:42.766[4397:9e2b] id -2- 2014-03-10 11:59:42.768[4397:9e2b] id -6- 2014-03-10 11:59:42.769[4397:9e2b] id -8- 2014-03-10 11:59:42.771[4397:9e2b] id -3- 2014-03-10 11:59:42.773[4397:9e2b] id -4- 2014-03-10 11:59:42.775[4397:9e2b] id -7-
so have in cards
array card
such idobject
, why predicate, return me card , no? what's wrong?
in card
entity property idobject
type of nsnumber
, change idobject
in predicate nsnumber
ie.
nspredicate *predicate = [nspredicate predicatewithformat:@"idobject == %@", [nsnumber numberwithint:[idobject integervalue]]];
or use nsnumberformatter
change idobject
nsnumber
.
hope you
Comments
Post a Comment