QuickBlox - How to get create custom query - quickblox

I need to get some Custom Objects but I need to check if two fields contain the value I pass.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:#"188888" forKey:#"user_id"];
[parameters setValue:#"188888" forKey:#"sent_to"];
[QBCustomObjects objectsWithClassName:#"Messages" extendedRequest:parameters delegate:self];
What I need is to check if the first parameter OR the second parameter are satisfied. With the code above I get a response as if a query was build like "user_id = 188888 AND sent_to = 188888".
Thanks in advance!

Operator OR is not supported in Custom Objects API yet.
I propose you to make 2 separates queries and than merge 2 results in client application

Related

How to keep data ordering after Core Data migration?

On current project I have a set of objects stored using Core Data.
Now I need to add one more field to DataModel.
I've added it, have created new mapping model, set it as current and successfully migrated to new data model. Everything works, but.. order of my data is lost.
For some weird reason previous developers doesn't create any order attribute to Data Model and it was sorted automatically when fetching data (by id I believe..)
After migration to new Data Model using mapping model I have random ordering of my data after each migration.
And some application logic related to order of this data is broken now.
How this issue can be resolved?
It is possible to create order attribute based on old ids?
Or how I can migrate with ids?
Already spent a day with this issue and didn't find the solution.
Thanks a lot!
Alex.
Requested retrieving data code:
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:#"Area" inManagedObjectContext:[self context]];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError *error;
return [[[self context] executeFetchRequest:request error:&error] mutableCopy];
The simplest way is not rely on some internal ordering. Add sort descriptors to the fetch request when you get access to data.
Also to be sure that your sorted results are processed in the sequential order, use old fashioned loop for (int i = 0; i < array.count; i++) instead of enumerators for (id a in array).
Finally figured out how to resolve this.
I've tried to create custom policy and found that data are passed to it in correct order (like it was stored in the CoreData before migration)
here is how to create custom policy: Core Data versioning and migrating with custom policy
After this things became simpler. I've created sortOrder field and increment it for each entity in the custom policy and set it to new field sortOrder of my entity.
Thanks everyone for help.
Here is the code:
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error {
MyEntity* myEntity = (MyEntity*)[NSEntityDescription insertNewObjectForEntityForName:[mapping destinationEntityName] inManagedObjectContext:[manager destinationContext]];
myEntity.myProp1 = [sInstance valueForKey:#"myProp1"];
//a lot of properties here..
//here is the solution - set sortOrder. Is is the property of custom policy class - one instance of this class is used for each entity migration
area.sortOrder = sortOrder;
int value = [sortOrder intValue];
sortOrder = [NSNumber numberWithInteger:value + 1];
[manager associateSourceInstance:sInstance withDestinationInstance:myEntity forEntityMapping:mapping];
return YES; }
Then I simply sort items on my view.

NSSortDescriptor which generates ORDER BY `name` = 'bob' DESC?

In raw SQL it would be simple to sort with all bobs coming first.
How can I do this using a NSFetchedResultsController and NSSortDescriptor?
I think that you cannot do that with NSSortDescriptor, because this is for key-value sorting and does not support expressions.
However, it could work with the help of NSFetchRequest. Set the result type of the fetch request to dictionary
fetchRequest.resultType = NSDictionaryResultType;
Then you can set the properties to fetch to the properties you want plus add a expression for computed properties:
NSExpression *expression = [NSExpression expressionWithFormat:#"name=='bob'"];
NSExpressionDescription *expressionDescription = [NSExpressionDescription new];
expressionDescription.name = "isCalledBob";
expressionDescription.expression = expression;
expressionDescription.resultType = NSBooleanAttributeType;
[fetchRequest propertiesToFetch:#[…, expressionDescription];
Then you can use NSSortDescriptor on the key isCalledBob.
With this you get dictionaries instead of managed objects.
Typed in Safari, did not test it, my kid wakes up in some minutes.
The solution I found to this is kind of problem is to just add an extra field to help with the FRC. ie isBob which gets updated whenever the name is set.

Can I construct a PFQuery that queries all classes?

i have an ios application in which i create a number of different classes of PFObjects, and i use pinning to the local datastore to take care of situations when i don't have network connectivity.
i'd like to query the local datastore from time to time in order to get all the objects in the store, irrespective of class.
i haven't been able to do this yet. the following code works fine and finds all the items of class MyClass
PFQuery *localStoreQuery = [[PFQuery alloc] initWithClassName:#"MyClass"];
[localStoreQuery fromLocalDatastore];
NSArray *results = [localStoreQuery findObjects];
but the following gives the error [Error]: bad characters in classname: (null) (Code: 103, Version: 1.8.5)
PFQuery *localStoreQuery = [[PFQuery alloc] init];
[localStoreQuery fromLocalDatastore];
NSArray *results = [localStoreQuery findObjects];
i also tried putting in #"*" as the classname like so
PFQuery *localStoreQuery = [[PFQuery alloc] initWithClassName:#"MyClass"];
but this also fails
so...is there any way to generically grab all pinned items of all classes, or do i have to have a loop and query each class i am creating separately (ugh)?
any help much appreciated.
Unfortunately you cannot. Parse does not support multi-class queries. You'd have to do each one, or make a super class that contains pointers to the object you'd like.

Fetch the smallest date of an object in CoreData

I'm having the same problem as this question, but the answer of #davedelong is not working for me.
When following the Apple Example, for fetching the smallest date in a set of object I get the following error
-[NSDate count]: unrecognized selector sent to instance
My understanding is that NSExpression's max: only support NSArrays. So I need an other solution.
#davedelong suggested using an ascending NSSortDescriptor, and so I did :
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:NSStringFromClass([GCSession class])
inManagedObjectContext:self.objectContext];
fetchRequest.fetchLimit = 1;
NSSortDescriptor* sort = [[NSSortDescriptor alloc] initWithKey:#"startDate" ascending:YES];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
GCSession* session = [[self.objectContext executeFetchRequest:fetchRequest error:nil] lastObject];
return session.startDate;
The problem here is that the session object returned from the fetch doesn't seems to be the one with the smallest startDate. In my tests, it seems that it even returned the newest date but it doesn't seems consistent.
I could also fetch every GCSession object and sort them but that seems way overkill, especially that GCSession will be augmenting in number when the users will use the application.
Edit : A test project that demonstrate the bug in Apple's example code.
A sort only comes into play after you've fetch the objects. You've set a fetch limit of one and no predicates which tells the fetch "go grab any random single GCSession object". One the fetch has an array 1 element long, it then sorts it, which is useless.
If you want to use a sort to find a min or max, you have to fetch all the objects and then sort them. Removing the line:
fetchRequest.fetchLimit = 1;
… should allow the code to work.
However, you should be able to fetch min and max values with expressions. It's kind of a basic operation.
Edit: Look at the comments for more detail, but Apple's example can work if you change the backing store to SQL instead of XML

How can I do a multidimensional lookup table in Objective-C?

New to Objective-C here...
I have a unique string code as the key that I'd like to use to look up associated values.
It seems an NSDictionary is what I'm looking for but I believe this is only used to lookup one value for a given key.
Can anyone provide the code on how to declare/fill a multidimensional immutable NSDictionary? Also the code to retrieve the values?
Also, please let me know I'm going about this the wrong way
Thanks
EDIT: example of array within a dictionary .... using example data from your comment.
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
NSArray *firstSet = [[NSArray alloc] initWithObjects:43, 22];
NSArray *secondSet = [[NSArray alloc] initWithObjects:32, 50];
[myDictionary setValue:firstSet forKey:#"010"];
[myDictionary setValue:secondSet forKey:#"011"];
// Get a value out - should be the 50 that you want
NSInteger *myValue = [[myDictionary objectForKey:#"011"] objectAtIndex:1];
Not tested - but should be 95% right. Does this make sense?
You can make any object you want be the value for a given key in an NSDictionary. This includes NSArray or even another NSDictionary. Using either of these would allow you to associate multiple values with one key.
For nested NSDictionaries or custom KVC-complient classes you can use keyPaths, and for nested NSArrays indexPaths.
Also stackoverflow will give you many examples.