We have a web resource where I wish to undertake a search, the results I want returned least price first.
The web response is returning things correctly; and in the order I want -- but then when I do a NSFetchRequest using an IN statement (where I use a NSSet) the order being returned is not in the same order I supply it and I'm not sure how to resolve it.
To keep things simple, I will use IDs;
The following is a response from server using IDs
{(
54916a82e677b8234499df30,
54916a23e677b8234499df2f,
54916938e677b8234499df2d,
5491696ae677b8234499df2e
)}
The order in the above set is correct.
Now when we do this:
NSFetchRequest *fr = [[NSFetchRequest alloc] initWithEntityName:[self entityName]];
fr.predicate = [NSPredicate predicateWithFormat:@"remoteID IN %@", orderedSet];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:fr error:&error];
I did a breakpoint -- the results array is no longer in the order I inputted.
po results
<_PFArray 0x7fd082a3f960>(
<EmptyLeg: 0x7fd080487af0> (entity: EmptyLeg; id: 0xd000000000800004 <x-coredata://953BCD04-9E4E-497C-8B31-32A58CCB0305/EmptyLeg/p32> ; data: {
remoteID = 54916938e677b8234499df2d;
}),
<EmptyLeg: 0x7fd0804db7f0> (entity: EmptyLeg; id: 0xd0000000007c0004 <x-coredata://953BCD04-9E4E-497C-8B31-32A58CCB0305/EmptyLeg/p31> ; data: {
remoteID = 5491696ae677b8234499df2e;
}),
<EmptyLeg: 0x7fd0829937b0> (entity: EmptyLeg; id: 0xd000000000880004 <x-coredata://953BCD04-9E4E-497C-8B31-32A58CCB0305/EmptyLeg/p34> ; data: {
remoteID = 54916a23e677b8234499df2f;
}),
<EmptyLeg: 0x7fd08291daa0> (entity: EmptyLeg; id: 0xd000000000840004 <x-coredata://953BCD04-9E4E-497C-8B31-32A58CCB0305/EmptyLeg/p33> ; data: {
remoteID = 54916a82e677b8234499df30;
})
)
We can see that 54916a82e677b8234499df30
is right at the bottom of the list when it should be at the top
I would like the NSFetchRequest to respect the order of which things are inputted, and thus respond with an output in the same order.
I shouldn't need to do further sorting because the Server is already returning the order as indicated above.
I believe its to do with this:
fr.predicate = [NSPredicate predicateWithFormat:@"remoteID IN %@", orderedSet];
The orderedSet
is a NSSet of items as listed above.
How do I ensure that the NSFetchRequest using the IN statement respects my ordering without further sorting.
I don't want to rely on using sort by remoteID because they aren't strictly numbers and may not be comparable as strings in the future.
Many thanks
The fetch request is below;
<NSFetchRequest: 0x7f9f2857dbb0> (entity: EmptyLeg; predicate: (remoteID IN {"54916a82e677b8234499df30", "54916a23e677b8234499df2f", "54916938e677b8234499df2d", "5491696ae677b8234499df2e"}); sortDescriptors: ((null)); type: NSManagedObjectResultType; )
It's very standard with SQL databases to not guarantee the order of returned rows unless you use the ORDER BY clause. Core Data is similar. If you want to guarantee the order you'll need to use a sort descriptor on your fetch request. Or, possibly, just sort them afterwards.