Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Monday, January 25, 2016

Reading Parse.com docs

I got less time to do my things today. Most of the day went by playing with my son as maid took leave today.
Most part of the remaining time i read parse docs. Learnt a lots of new things about parse and also got some ideas out of it for my app.
When i turned on my system to work I managed to get the comment thing sorted out.
Actually i never mentioned it before here. So here it goes.
In MySQL i was using id for the content that were integers and they were getting incremented automatically on every new row added. I was using this id in the comments table to label which content this comment belongs to. Easy stuff that is.
But here in parse they use NoSQL database that doesn't give auto incrementing integers as id rather they give a alphanumeric objectId. So if i add a new row to content it will not generate same type of integer id so i will have to use objectId in comment table to link comments to newer content.
Now the problem arises how to write the query because old content will not have new objectId in comments and new on will not have the old content id.
To sort this out i needed to add objectId of the content to all the respective comments. I couldn't do it manually because there were 22,000 comments in all.
I needed an automated approach.
I had two options:
1. I use the cloud code facility that parse provide. To use it i needed to write a JavaScript function and run it on the data.
2. I extend my existing android code and update the ids using the app.
First option was little difficult for me as i have never done any of that stuff before. I mean i never used parse cloud code or the tools required to write cloud code. And also i never wrote such a JavaScript function before. But it could have been a nice learning experience.
I chose second option because it required very less time in comparison to first one and also i could track the changes i was introducing with android logcat, so i would not corrupt the data. Although i backed up all data before hand.
I wrote a java method that pulled all the comments attached to old content id and then update each of them by inserting new content objectId in it. I could have done it to all the data at once but i chose to go with one at a time approach. I tapped on the content and all the comment rows related to that content got updated and i tracked it in the parse interface and logcat window.
Took time but finally managed to update all.
Finally i updated the query to use the objectId rather than old content id in the android app to pull the comments. Working nicely.
Yesterday i updated the android app to send objectId for new comments so nothing was required to change in that method. 
Reading through the parse documents i learnt about the caching the data in device so i won't have to query it every time i needed more data. Nice function. Would use it. It will make things faster. Tomorrow will try to implement this caching thing.
One thing that went unexpectedly nice as i switched to parse is that when i was using JSON to get and sent data to and from app i was not getting the emoji working. They were changing into question marks. But now i can use all the emoji in content as well as in comments 🙌.
Later.

Thursday, January 21, 2016

Lazy day

Today was cold and lazy day.
I wanted to just lay down and not do anything, but you know.. 😔.
So i managed to import the api data to app engine datastore by pulling and parsing JSON from my current api. Actually i have imported 1 table and 1 more yet to be imported but that is not important. Even if i don't import it, it won't effect a thing but i will try to do that just for the sake of learning.
I tested the new api with my app and its working flawlessly.
I imported the data in local datastore, but the script will import it well in production server when i upload the project to app engine. Tomorrow i will be making a new app engine project and upload the api files to test it again.
I also learnt how to use memcache on app engine. Although it was just few line of code but it makes a huge difference in performance and server costs. What you do here is that you check if the data you looking for is saved in the memcache or not. If its there you serve it right away from memcache. If not you pull it from the datastore and save a copy in  memcache and also serve a copy to user. When next time that data is needed you pull it from the memcache and serve it without doing the roundtrip to the datastore. Memcache, as the name suggest is space in system memory(RAM) that is very fast compared to the disk or datastore so performance improvement is very good. And also app engine gives you restricted queries to datastore in free plan so you save that too by using memcache. If you Move to paid plan you will save cost by using memcache effectively.
I think i need to again read the app engine docs. I read it long back and now i have to look up every time i plan to do anything. This looking into is taking too much time and slowing me down considerably. 
Parsing JSON with python was new experience to me. I used simplejson to parse it. That doesn't mean i have learnt it. I found a sample code and adapted it to work by trial and error but afterward when i looked into the code it looked just like java code i wrote in my android app to parse the same JSON. Just a little syntax difference. Will read little more about simplejson. It will come very handy in long run.
As i have never learnt python in the way i should have, i feel little under confident writing the code but as i write it more i find it far more easier to write and way cleaner than either PHP or java. And writing app engine apps using webapp2 is a bliss compared to writing a vanilla PHP code.
In other news ,for some strange reason i am finding that my system is not loading few websites. It just keep on loading and doesn't show anything. If i try same pages from my phone using the same WiFi connection it loads in a jiffy. Don't know what's the problem. Will try to fix it. May be the problem is in the DNS. If it is then its a quick fix. If not then its going to be headache for me. Just chimed in that the problem may be in the host file i edited recently. Will look into it tomorrow.
What more..
Yes I updated more softwares.
Enough typing on phone on this cold and lazy day.
See ya..