Parse & Android: recommendations for novice developers

annotation


In this article, I would like to share my general impressions of using BaaS - a solution called Parse for developing the Android application backend, to talk about all the “pitfalls” I had to face during the development period. For the first time this platform was recommended to me by my colleagues when I was a junior, and there was only 1 commercial project behind me. The motivation for writing this article was the nerves and time that I spent searching for compatible versions of libraries and thinking about strange decisions of platform developers, or simply because I haven’t yet found articles on this . Also, I will not say anything about what Parse is and how to connect and configure it, but just in case I will leave all the necessary links where you can read about it.

Problem # 1: Using Parse Server in conjunction with PostgreSQl


The use of this configuration was due to the fact that the server was deployed on a VDS hosting service, and the use of a remote MLab database was impractical, because at the time of development Roskomnadzor tried to block Telegram in Russia, and there were problems with the connection without VPN. There was no time to configure VPN on the console Linux, and the project was on fire, so I decided to use a local database on a server. I chose PostgreSQL because I had a good experience with it.

Layfkhak No. 1: that the base worked without errors in data types, at the postgres installation it is necessary to install postgis. After that, you need to create a database and immediately after creation, create a connection to all postgis extensions. About how to connect the expansion postgis to the base, can be read here. After all extensions are connected, you can connect the database to the server, open the dashboard and see that the tables were created without errors.

Life hacking number 2:use the Parse server version> = 2.7.2. When I downloaded the test project from the gita, there was a server of version 2.2.5, and everything seemed to work, but later one bug got out: while maintaining the geolocation coordinates, lat and lng were swapped. And there were 2 cases: if the coordinates were <90 by module, then the marker on the map was just in another place, otherwise the application caught crash, and a log stating that Lat should not be greater than 90 by module fell into the console. Then I started a 2-day tupnyak in search of solutions. What I just did not find on various forums and in github issues: a coup of coordinates in the Cloud-function (does not work!); the change of coordinates in the PostgresStorageAdapter (after the changes a bunch of errors arose, I did not want to delve into the end of the working day, turned off the computer and left). The next day I looked at the releases, and saw that in version 2.7. 2 a bug was fixed in the PostgresStorageAdapter. Quickly fixed the version in package.json, and lo and behold, everything worked as it should. At this point, version 3.xx was already there, and I tried to use it, but the developers made a lot of changes related to the Cloud-functions, and another bunch of errors spilled out at the start. There was no time to correct the working code, so version 2.7.2 came to me just in time. If you just started your project, then of course it is better to use the latest version.

Problem number 2: LiveQuery does not unsubscribe


I spent a little more than one day to solve this problem. And she was damn weird and not obvious.

Initially, the architecture was like this:

publicclassSubclassextendsParseObject{
    // init columns, create getters & setters
}
publicclassQueryHelper{
    publicstatic ParseQuery getQuery(Param... params){
        ParseQuery query = ParseQuery.getQuery(Subclass.class);
        // init query by paramsreturn query;
    }
}
publicclassMainActivityextendsActivity{
    publicvoidonResume(){
        ParseLiveQueryClient.getClient().subscribe(QueryHelper.getQuery(), callback);
    }
     publicvoidonPause(){
        ParseLiveQueryClient.getClient().unsubscribe(QueryHelper.getQuery(), callback);
    }
}

And at an exit from the screen the method was caused, but the request was not unsubscribed. As you know, LiveQuery is signed on request, and any change in the data matching the request can be tracked in the callback. Unsubscribe also happens on request. In the subscription method, a Subscriber object is returned, but this object is absolutely useless, because it does not contain the “unsubscribe” method, and LiveQueryClient itself does not contain the “unsubscribe” method with the Subscriber parameter. Turning on the debug, I started step by step into the same method of "unsubscribe". In the client itself is stored in private list of subscriptions. In the method, developers cycle through this sheet and compare the request from the parameter with a private request that is stored in the subscription object by the unredefined function equals, which corresponds to the usual ==, and which compares the addresses of complex objects. And that explained everything because in my project there was a class with functions that created the necessary request for me. And since the request object was always created anew, therefore, the requests for the requests were different, equals did not work, and the reply did not occur. I solved this problem in the following way: I made singleton, and it all worked.

It looked like this:

publicclassSubclassextendsParseObject{
    // init columns, create getters & setters
}
publicclassQueryHelper{
    publicstaticfinal ParseQuery query;
    publicstatic ParseQuery getQuery(Param... params){
        if (query == null) {
            query = ParseQuery.getQuery(Subclass.class);
            // init query by params
        }
        return query;
    }
}
publicclassMainActivityextendsActivity{
    publicvoidonResume(){
        ParseLiveQueryClient.getClient().subscribe(QueryHelper.getQuery(), callback);
    }
     publicvoidonPause(){
        ParseLiveQueryClient.getClient().unsubscribe(QueryHelper.getQuery(), callback);
    }
}

After some time, the idea came about writing my own manager, who will follow the subscriptions, but I never implemented it.

Conclusion


I hope this article will be useful. If you find any inaccuracies or errors, write to me. As promised, I will leave links to several good sources that have helped me:


Good luck to all!

Also popular now: