NSPredicate query for a Realm object - objective-c

Could someone please help me construct a realm query to search for a video(s) of a particular entryid value, for extra points a second query that would find both videos by their entryids ?
Thank you!
Video {
nid = 1;
video_title = OK;
video_description = Description {
value = Description;
};
video_kaltura = Kaltura {
entryid = 0_abababa3;
mediatype = 1;
};
}
Video {
nid = 2;
video_title = OK;
video_description = Description {
value = Description;
};
video_kaltura = Kaltura {
entryid = 0_cdcdcdc3;
mediatype = 1;
};
}
I've tried things like:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"video_kaltura['entryid'] CONTAINS[c] '0_abababa3'"];
RLMResults *test = [Video objectsWithPredicate:pred];
and their variations to no avail.

NSPredicate uses key paths to traverse properties (object.someProperty.otherProperty).
Based on your example data, you'd want a predicate like video_kalutra.entryid == '0_abababa3'.

Related

How get a key value in NSMutableArray?

A NSMutableArray called arrayDB, and it gets data from server by using AFNetworking, like this
[self.arrayDB addObjectsFromArray:[responseObject objectForKey:#"list"]];
NSLog(#"print:%#",self.arrayDB);
And the result like this.
print:(
{
id = 10;
key = 7707eca6ea4d2db923c8b7b4e6ec094c;
},
{
id = 9;
key = 7aaf962bc4df61a3b44c544c58fc6c82;
},
{
id = 7;
key = ce9d9e99a96d6417c6b34321c820c4c0;
},
{
id = 6;
key = 6086663465e5813d08f862eb443e2623;
},
{
id = 4;
key = 5d9519dd7de26f0139d6028fb83a4ead;
}
)
And now, I use sql statement to describe my needs, select key from arrayDB where id=10, how can I write in objective-C?
You can use NSPredicate like this:
NSArray *myArray = [self.arrayDB filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(id == %#)", #"10"]];

Get the Array list based on the duplicate values in that Array Objective C

i have stuck up with below problem. I have got the below response.
(
{
CarrId = 102;
CarrName = "Fast and Furious";
CarrOptions = (
{
Id = 8;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "S-30";
},
{
Id = 9;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "M-30";
},
{
Id = 10;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "S-30";
},
{
Id = 11;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "M-30";
},
{
Id = 12;
Img = "603.jpg";
PImg = "611.jpg";
PQ = "L-30";
},
{
Id = 13;
Img = "738.jpg";
PImg = "749.jpg";
PQ = "S-30";
},
{
Id = 14;
Img = "738.jpg";
PImg = "749.jpg";
PQ = "M-30";
}
);
Status = 1;
}
)
Get the Array list based on the duplicate values in that Array Objective C. I need to get the array based on the PImg. For example if PImg = 412.jpg. i need the array something lik below
{
Id = 8;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "S-30";
},
{
Id = 9;
Img = "400.jpg";
PImg = "412.jpg";
PQ = "M-30";
}
I have only done like getting the duplicates from the response
NSMutableDictionary * thisRow = [resultArray objectAtIndex:0];
NSMutableArray *pdctDtls = [thisRow objectForKey:#"CarrOptions"];
NSOrderedSet *orderedSet = [[NSOrderedSet orderedSetWithArray:pdctDtls] valueForKey:#"PImg"];
NSMutableArray *newarray = [orderedSet mutableCopy];
In newarray am getting the duplicate values. but if in the newarray if 412.jpg get the array related to 412.jpg. TIA
Try this. If you pass 611.jpg, you get 3 objects.
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF CONTAINS[c] %#", #"611.jpg"]; // AS PER YOUR REQUIREMENT
NSArray *array =[pdctDtls filteredArrayUsingPredicate:predicate];
You could use an NSPredicate like so:
NSArray *pdctDtls = thisRow[#"CarrOptions"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"PImg = %#", #"412.jpg"];
NSArray *resultsWithSamePimg = [pdctDtls filteredArrayUsingPredicate:predicate];
if (resultsWithSamePimg.count > 1) {
// we have some duplicates
// ...
}
But I would rather use a plain old for loop, which might be easier to debug and understand.

I'm having trouble accessing the value I need from an NSDictionary object

I'm having trouble accessing the value I need in an NSDictionary object, a song has multiple values under the track key, but since dictionaries aren't indexable, I'm unable to access the values individually. I have tried storing the object for the track key as an array but the two entries become a single string entry and therefore the array is only of length 1 and not helpful.
Also, doing something like [response valueForKeyPath#"response.songs.tracks.foreign_id"] gives me both foreign ids, how can I just get the first one? I've been struggling with this for a while, have searched all around stackoverflow for answers but none have worked, any help is appreciated, thanks!
This is the data in the NSDictionary:
{
response = {
songs = (
{
"artist_foreign_ids" = (
{
catalog = spotify;
"foreign_id" = "spotify:artist:6vWDO969PvNqNYHIOW5v0m";
}
);
"artist_id" = AR65K7A1187FB4DAA4;
"artist_name" = "Beyonc\U00e9";
id = SOUTJIT142F256C3B5;
title = "Partition (Explicit Version)";
tracks = (
{
catalog = spotify;
"foreign_id" = "spotify:track:2vPTtiR7x7T6Lr17CE2FAE"; //I WANT TO ACCESS JUST THIS VALUE
"foreign_release_id" = "spotify:album:1hq4Vrcbua3DDBLhuWFEVQ";
id = TRLHJRD1460052F846;
},
{
catalog = spotify;
"foreign_id" = "spotify:track:6m4ZFQb3zPt3IdRDTN3tfb";
"foreign_release_id" = "spotify:album:5KPpho7rztJrZVA9yXjk8K";
id = TRYMBSK146005C3A46;
}
);
}
);
status = {
code = 0;
message = Success;
version = "4.2";
};
};
}
The tracks in your sample dictionary appear to already be an array (of dictionaries), so you should be able to do something like:
NSArray *songs = response[#"songs"];
for (NSDictionary *songDict in songs) {
NSArray *tracks = songDict[#"tracks"];
for (NSDictionary *trackDict in tracks) {
NSString *trackForeignId = trackDict[#"foreign_id"];
// Do whatever you like with trackForeignId here.
}
}

Predicating Nested Array

Doing a project on addressbook kind of app. I need to predicate address book contacts, result looks like this
(
{
AddressKey = (
);
Email = (
);
"JobTitle_Name" = "";
"Organisation_Name" = "";
Phone = (
{
phoneNumber = "+919502266633";
"phone_type" = home;
},
{
phoneNumber = 9703570333;
"phone_type" = work;
},
{
phoneNumber = 91234512345;
"phone_type" = iPhone;
},
{
phoneNumber = 91239123;
"phone_type" = mobile;
}
);
"first_name" = Raviraja;
imageKey = "";
"last_name" = "";
serialNumberKey = 53;
source = Device;
}
)
Need to predicate the array using phoneNumber key. I tried this one
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY %K == %#",#"phoneNumber",[NSString stringWithFormat:#"%#",str]];
NSArray *array = [contactsArray filteredArrayUsingPredicate:predicate];
but not working eventhough phone number is matching.
Help me out guys.
The key path is wrong, it should be
[NSPredicate predicateWithFormat:#"ANY %K == %#",#"Phone.phoneNumber", ...]

Searching within subsubdictionaries of an array to return the parent dictionary (Objective C)

I have a structured array like the one below
(
{
id = 0;
name = "A";
tables = (
{
comment = "";
id = 0;
name = "T1";
},
{
comment = "";
id = 1;
name = "T2";
},
{
comment = "";
id = 4;
name = "T3";
}
);
},
{
id = 1;
name = "B";
tables = (
{
comment = "";
id = 5;
name = "T1";
},
{
comment = "";
id = 6;
name = "T2";
}
);
}
)
Given that I know the value of id key of one of the dictionaries (let's take id=6), how can I get its parent dictionary or to be more precise the value of name key (in this case B).
I've tried using predicate strings but the problem is that the parent dictionary has also a key named id, so I get the wrong results.
EDIT: id keys are unique (and it is also not necessary that ids of the elements in the first dictionary have lower intiger values than the ones in the dictionaries with higher indexes)
Try this, where array is your structured array
NSDictionary *parentDict=[[NSDictionary alloc]initWithDictionary:nil];
NSString *name=[[NSString alloc]init];
NSArray *table=[[NSArray alloc]initWithArray:nil];
for (NSDictionary * dict in array) {
table=[dict objectForKey:#"tables"];
for (NSDictionary *childDict in table) {
if ([[childDict objectForKey:#"id"]isEqualToString:#"6"]) {
//if your id value is int then use ==6 to check
parentDict=dict;
name=[dict objectForKey:#"name"];
return;
}
}
}