2

In Deals Table the attributes:

ispopular(attribute)
groupname(attribute)
dealsassets(relationship-name to assets)

In Assets Table the attributes are :

assettype(attribute)
caption(attribute)
dealassetid(attribute)
assetsdeal(inverse relationshipname to deals)

Deals is Assets one to many relationship & Assets to Deals many to one relatiosnhip

I want to write a query where i need is ispopular == 1 then that related field's assets.dealasseti,

what predicate query i have to write, could some one help me out.

regards

iOSDev
  • 412
  • 10
  • 30

1 Answers1

3

Is this what you are looking for?

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Assets"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"assetsdeal.ispopular == 1"];
[request setPredicate:predicate];

NSError *error;
NSArray *result = [context executeFetchRequest:request error:&error];

Using the inverse relationship you ask for all assets where the related deal has the property "ispopular == 1".


Alternative Solution (if the first one does not work due to some StackMob restrictions): Fetch the deals with "ispopular == 1" first:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Deals"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ispopular == 1"];
[request setPredicate:predicate];

NSError *error;
NSArray *deals = [context executeFetchRequest:request error:&error];

and use Key-Value Coding to get the related assets:

NSArray *assets = [deals valueForKeyPath:@"dealsassets.@distinctUnionOfSets.self"]
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • 1
    @Firo: Both "=" and "==" work. But if you NSLog a predicate then "==" is displayed for the equality operator, so I tend to use that. – Martin R Jun 21 '13 at 13:02
  • @MartinR : i am getting this crash log : Error fetching: Error Domain=HTTP Code=400 "The operation couldn’t be completed. (HTTP error 400.)" UserInfo=0xac6bb80 {error=Field assetsdeal.ispopular does not exist in assets} – iOSDev Jun 21 '13 at 13:04
  • 2
    @iOSDev: You said that the Assets entity has a relationship "assetsdeal" to Deals, and that Deals has a property "ispopular". Perhaps you can show the contents of Assets.h and Deals.h or show a screenshot of the Core Data properties. - And how is this related to HTTP ?? – Martin R Jun 21 '13 at 13:11
  • @MartinR : I have added the screenshot could u check it – iOSDev Jun 21 '13 at 13:40
  • 1
    @iOSDev: The model looks OK. But could it be **that you are using StackMob?** If yes, why didn't you tell us in your question? - I have no experience with StackMob, but according to this answer: http://stackoverflow.com/a/16112734/1187415, fetching on an attribute of a relationship is not supported. – Martin R Jun 21 '13 at 13:55
  • @MartinR : Yeah i am using Stackmob, thinking that coredata and stackmob work in the same way, so how can u get the value.. is there any other option. – iOSDev Jun 21 '13 at 14:17
  • @iOSDev: (I should get extra points for guessing correctly :-) ... I have added an alternative solution. But as I said, I have no experience with StackMob therefore I cannot say if that will work or not. – Martin R Jun 21 '13 at 14:29
  • @MartinR : That u deserve, I was taken back when u said about stackmob – iOSDev Jun 21 '13 at 14:39
  • @MartinR : I Have used the alternative solution which u said, this i have already tried, and fell to the same problem once again, i am getting this as my output http://pastebin.com/gLrapcQv – iOSDev Jun 21 '13 at 14:51
  • @iOSDev: Why the same problem? Previously you had a crash, and now you get some output! - And that output doesn't look so bad. `"{(\n)}",` looks like the output from deal without any asset. Did you try `[deals valueForKeyPath:@"dealsassets.@distinctUnionOfSets.self"]` as suggested in my answer? – Martin R Jun 21 '13 at 15:10
  • @MartinR : No, I have tried with this NSArray *assets = [deals valueForKeyPath:@"dealsassets.dealassetid"]; – iOSDev Jun 21 '13 at 15:16
  • @MartinR: I have tried with this just now, [deals valueForKeyPath:@"dealsassets.@distinctUnionOfSets.self"], got the result http://pastebin.com/rf3bCWgU, I am getting the objects of Assets table, i need previoous output only, and if u see in this pastebin.com/gLrapcQv in the last, we have some urls present, i need to put all those urls in array. – iOSDev Jun 21 '13 at 15:19
  • 1
    @iOSDev: So you need only the list of asset ids? Try `[deals valueForKeyPath:@"dealsassets.@distinctUnionOfSets.dealassetid"]`. – Martin R Jun 21 '13 at 15:23
  • @MartinR : This Superb thanks a Lot Man... and Martin could you just throw some light what [deals valueForKeyPath:@"dealsassets.@distinctUnionOfSets.dealassetid"] does ?? – iOSDev Jun 21 '13 at 15:36
  • @MartinR : We should also consider the performance right?, I am getting the required output, the performance is getting low, its taking time to get all the urls.. can we do anything to reduce this ?? – iOSDev Jun 21 '13 at 15:43
  • @iOSDev: For `@distinctUnionOfSets`, have a look at the [Key-Value Coding Programming Guide](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html%23//apple_ref/doc/uid/10000107-SW1). I do not have a good idea for performance improvements, in particular because that might be a StackMob issue and I don't have any knowledge about StackMob. May I suggest that you start a new question dedicated to that problem? Then other people (who might know more about StackMob) will see it and can provide an answer. – Martin R Jun 21 '13 at 16:10