i have 3 entities this: publisher <-------->> author <-------->> book
publisher{ id = <uniqueid> authors -->> author } author{ id = <uniqueid> books -->> book //ordered relationship publisher --> publisher } book{ info = <string_with_info> author --> author }
i want filter books author id , publisher id , info attribute . in lines of:
nspredicate *predicate = [nspredicate predicatewithformat:@"author.publisher.id == %@ , author.id == %@", publisherid, authorid]; [request setpredicate:predicate]; [request setentity:[nsentitydescription entityforname:@"book" inmanagedobjectcontext:managedcontext]]; [request setresulttype:nsdictionaryresulttype]; [request setreturnsdistinctresults:yes]; [request setpropertiestofetch:@[@"info"]]; nserror *error = nil; nsarray *results = [managedcontext executefetchrequest:request error:&error];
where result should contain many strings books infos. not sure author.publisher.id == %@
part works.
i author , mutableset books entities. loop on them , push info new array. job same fetch if possible.
edit: solution works changing predicate so:
nspredicate *predicate = [nspredicate predicatewithformat:@"(0 != subquery(author, $x, (0 != subquery($x.publisher, $y, $y.id %@).@count)).@count) , author.id %@ ", publisherid, authorid];
some clarification. author id tied publisher , unique publisher. have 2 authors same id different publisher.
expand upon original question. author books ordered. infos in results
ordered in same order books ordered in author?. answer per limited test yes.
the possible id conflict has been pointed out mundi , it's been noted.
first, after clarifying authorid not unique, need use original compound predicate:
nspredicate(format: "author.publisher = %@ && author.authorid = %@", publisher, authorid)
btw, replace attribute "id" else because there potential name conflicts "id".
but can fetch author, making predicate more concise:
nspredicate(format: "publisher = %@ && authorid = %@", publisher, authorid)
second, books set, no need distinct results. actually, without explicit fetch if have author object.
author.books.map { $0.info }
is need. behold how elegant swift can be!
Comments
Post a Comment