Quantcast
Viewing all articles
Browse latest Browse all 8068

Keep collection synced with remote database

I know there are a few question about this topic, but none have really helped me to understand how it works and how I should be implementing it. If at all possible I want to avoid using a third party REST API.

I have the collection set up, it gets data from the remote database and saves the models fine. However, when I leave the controller and create it again (ie. logout and login), there is currently no check to see if the data returned from the HTTPClient contains duplicate records, and so it just keeps adding to the collection.

I imagine I should be looping through the returned data and check each record to see if it already exists in the collection and skip it, as well as check if any models in the collection do not match any of the returned records and remove them. I thought about simply resetting the collection each time (I couldn't even get that to work) but I didn't want it to leave the user with an empty collection if they accessed it without a connection (they could still be logged in using a cookie).

I also tried using the contains() function from Backbone to at least prevent duplicates from being added, but I guess that isn't implemented in the version used by Titanium (3.2.0).

So how could I go about ensuring the collection is synced with the remote database each time the controller is opened?


Viewing all articles
Browse latest Browse all 8068

Trending Articles