how to Sort jsonDoc by Created Date in iOS ?

0 votes

I want to sort  all json documents by  descending order of their created date . For this , I've written :

    NSMutableDictionary *otherMetaHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"orderByDescending",@"createdAt", nil];

    StorageService *storageService = [App42API buildStorageService];

    [storageService setOtherMetaHeaders:otherMetaHeaders];

   [storageService findAllDocuments:<dbName> collectionName:<collectionName> completionBlock:^(BOOL success, id responseObj, App42Exception *exception)

{

...

..

...

}

 

But the docs are not getting in the required order . pls help  

asked Oct 1, 2015 in App42 Cloud API-BaaS by gtarun16 (11 points)

1 Answer

+1 vote
Hello Tarun,

 

Everthing in your code snippet is correct except the value passed place or oderByDescending. Please update value from "createdAt" to "_$createdAt". It will work for you.

Let me know if you need any help from my side.

Regards,

Himanshu Sharma
answered Oct 1, 2015 by hs00105 (2,005 points)
Thanks Himanshu , but the change you suggested is also NOT working for me :(
Hello Tarun,

Can you print internal logs of App42 method and share with us. To print the internal logs, put the below line of code before making a request of App42 API:
 [App42API enableApp42Trace:YES];

Regards,
Himanshu Sharma
Hi Himanshu ,
I printed internal logs , the log data is huge and contains my app db info , do you require any specific value out of logs or entire logs ???
Hello Tarun,

You can share that information over email at support@shephertz.com.

Regards,
Himanshu Sharma
App42Trace: -[App42Service populateSignParams].....IN

App42Trace: UTCTimeStamp=2015-10-01T10:41:28.298Z

App42Trace: -[App42Service populateSignParams].....OUT

App42Trace: -[App42Service populateMetaHeaderParams].....IN

App42Trace: InstallationId=5c70bd23979826a683215a81b50f7f14

App42Trace: -[App42Service populateMetaHeaderParams].....OUT

App42Trace: MetaHeaders={

    SDKName = iOS;

    "_$createdAt" = orderByDescending;

    deviceId = <device_id>;

}

App42Trace: -[Connector executeGetWithURL:signParams:queryParams:andHeaderParams:completionBlock:]

App42Trace: signParams={

    apiKey = <api_key>;

    collectionName = <collection_name>;

    dbName = <db_name>;

    sessionId = “<session_id>";

    timeStamp = "2015-10-01T10:41:28.298Z";

    version = "1.0";

}

App42Trace: urlString=https://api.shephertz.com/cloud/1.0/storage/findAll/dbName/<dbname>/collectionName/Notifications?

App42Trace: headerParams={

    SDKName = iOS;

    "_$createdAt" = orderByDescending;

    apiKey = <api_key>;

    deviceId = <device_id>;

    sessionId = “<session_id>";

    signature = "EIW1F%2BEs55HScw1ZY74obZ0nBwI%3D";

    timeStamp = "2015-10-01T10:41:28.298Z";

    version = "1.0";

}

App42Trace: queryParams={

}

App42Trace: Policy is APP42_NO_CACHE

App42Trace: Response STring={"app42":{"response":{"success":true,"storage":{"dbName”:”<db_name>","collectionName":"Notifications","jsonDoc":[{"_id":{"$oid”:”<doc_id>"},"sendername":"Tarun G","_$owner":{"owner":"gtarun@gmail.com"},"senderemail":"gtarun@gmail.com","_$createdAt":"2015-10-01T07:56:56.774Z","_$updatedAt":"2015-10-01T07:56:56.774Z",}]}}}}

App42Trace: __64-[Connector handleAsyncResponse:withData:error:completionBlock:]_block_invoke..responseString={"app42":{"response":{"success":true,"storage":{"dbName”:”<db_name>","collectionName":"Notifications","jsonDoc":[{"_id":{"$oid”:”<doc_id>"},"sendername":"Tarun G","_$owner":{"owner":"gtarun@gmail.com"},"_$createdAt":"2015-10-01T07:56:56.774Z","_$updatedAt":"2015-10-01T07:56:56.774Z"},{"_id":{"$oid":”<doc_id>"},"sendername”:"T arun","_$owner":{"owner":"gtarun@gmail.com”},"_$createdAt":"2015-10-01T08:04:47.539Z","_$updatedAt":"2015-10-01T08:04:47.539Z"},{"_id":{"$oid”:”<doc_id>"},"_$owner":{"owner":"gtarun@gmail.com"},"_$createdAt":"2015-10-01T08:08:06.825Z","_$updatedAt":"2015-10-01T08:08:06.825Z"},{"_id":{"$oid”:”<doc_id>"},"sendername”:"Tarun","_$createdAt":"2015-10-01T08:34:46.250Z","_$updatedAt":"2015-10-01T08:34:46.250Z"},{"_id":{"$oid”:”<doc_id>"},"sendername”:"Tarun","_$createdAt":"2015-10-01T10:26:36.161Z","_$updatedAt":"2015-10-01T10:26:36.161Z"}]}}}}

App42Trace: Response = <NSHTTPURLResponse: 0x1652ab50> { URL: https://api.shephertz.com/cloud/1.0/storage/findAll/dbName/<db_name>/collectionName/Notifications? } { status code: 200, headers {

    "Access-Control-Allow-Credentials" = true;

    "Access-Control-Allow-Headers" = "Content-Type,Accept,apiKey,signature,timeStamp,sessionId,adminKey,fbAccessToken,dataACL,geoTag,selectKeys,SDKName,deviceId,event,version,userList,deletePermanent,loggedInUser,dbCredentials,metaQuery,emailAuth,orderByDescending,orderByAscending,jsonObject,offset,max,dataEncoding,like,emailVerification,userProfile,customName,userName,tag,template,isAttachScoreId,setFBImageDimension,X-Access-Token, X-Application-Name, X-Request-Sent-Time,Custom-Headers,outputCollection,description,groupMetaInfo,jsonQuery,pageMaxRecords,pageOffset,pushIdentifier";

    "Access-Control-Allow-Methods" = "GET, POST,HEAD, OPTIONS,PUT, DELETE";

    "Access-Control-Allow-Origin" = "*";

    "Access-Control-Max-Age" = 10;

    "Cache-Control" = "no-cache";

    Connection = "keep-alive";

    "Content-Type" = "application/json";

    Date = "Thu, 01 Oct 2015 10:41:27 GMT";

    Server = "Apache-Coyote/1.1";

    "Transfer-Encoding" = Identity;

} }

App42Trace: Exception = (null)
Hello Tarun,

Apologies from my side that i have not notice before correcting your code snippet. You have passed the value in wrong way. Please update the below line of code:
from:

NSMutableDictionary *otherMetaHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"orderByDescending",@"createdAt", nil];

To
NSMutableDictionary *otherMetaHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"createdAt",@"orderByDescending", nil];

Let me know if you still face this issue.

Regards,
Himanshu Sharma
I changed it to :
    NSMutableDictionary *otherMetaHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"_$createdAt",@"orderByDescending", nil];

This is WORKING now , Thanks Himanshu .

So ,I think the tutorial pointing in url ( http://api.shephertz.com/app42-docs/nosql-storage-service/#sorting-of-json-documents ) saying :
NSMutableDictionary *otherMetaHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"orderByAscending",@"<Name_Of_key_You_Want_To_Sort>", nil];  

is wrong and the tutorial needs to be updated I guess .
Thanks for pointing out to this, Will update this in our future release. Let me know if you need any other help from my side.

Regards,
Himanshu Sharma
Thats it Himanshu, Thanks :)
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...