Start a new topic

Offline support Titanium/Backbone

Hey,



quick try out to implement offline-support results in calling server each single time, instead of just the first one ?!





Tried:

sync : {

enable : true,

online : Titanium.Network.getOnline() // The initial application state.

}



Set this on Startup/Connecting to the Kinvey Backend







and this to fetch the Collection from server, or if available, use the stored one:

/...

myEvents.fetch({

maxAge : 3600,

offline : true,

fallback : true,

refresh : true,



query : eventsQuery,

/...



Where is the mistake?





best, Nico

(corresponding to the docs, this should work like expected, or?)






Note it is `KINVEY_DEBUG`, not `Kinvey.Debug`. Regarding relational data, yes, the cache does support this. There might be limitations, do you have an example of the relational query you’re trying to run?
Ok I'm getting closer i think...



1. Turning Kinvey.Debug = true causes the crash... Don't know where, but if i set it to false, it doesn't crash.





2. My request with offline:true and maxage works - but does the cache use relational data?

Because - for me - it doesn't seem to be the case and theres no hint in the documentation.





Best, Nico
Hey,





i've tried to get some informations about the crash of the application - but i can't get any... :/

Settings:

Maxage: 100 and offline : true





Simulator:

(First Start) Trying to load data - crashes istantly. The logs (kinvey.debug = true) telling me about the request, but after that the Simulator just shut down.



On Device:

The first Call is successfull - but than requesting again resulting in a crash of the application.



The xcode Logs aren't very helpful at all ...



> Jul 13 13:05:34 Nicos-iPhone ReportCrash[783] : ReportCrash acting against PID 780

> Jul 13 13:05:35 Nicos-iPhone ReportCrash[783] : Formulating crash report for process APP[780]

> Jul 13 13:05:35 Nicos-iPhone com.apple.launchd[1] (UIKitApplication:com.****.app[0x266d][780]) : (UIKitApplication:com.****.app[0x266d]) Job appears to have crashed: Bus error: 10

> Jul 13 13:05:35 Nicos-iPhone backboardd[28] : Application 'UIKitApplication:com.*****.app[0x266d]' exited abnormally with signal 10: Bus error: 10

>
Are you able to get the error message when Titanium crashes? Obviously, something does go wrong here, which may be a thing I can fix in the library.



To answer your question, the note in the docs means that if you fetch a collection from cache, the `fallback` property will do nothing. While, if you are fetching a single entity, the `fallback` option will force to issue a network request when the entity cannot be found locally. I don’t think you need the `fallback` / `refresh` flag, the defaults should work for you. The `offline: true` and `maxAge` should be it, but again, if it is crashing something goes wrong.
Addition:



With



maxAge : 20,

refresh : true,

fallback : true,



as attributes the backbone sdk crashes on Titanium.



So i think it's a bug and should be noticed in the docs?!



2. Question:

Regarding to the docs - is it possible to pull a collection from cache? Cause there is an info box telling something that only entititys called by id could do this ... ?
Hi,



that sounds clear, but didn't work for me...





Example Code that i am using:

if (Alloy.Globals.Collections.notes.length == 0) {

promise = Alloy.Globals.Collections.notes.fetch({



});

} else {

promise = Alloy.Globals.Collections.notes.fetch({

maxAge : 10,

offline : true,

refresh : true,

fallback : true

});

}







So if i load the data (first time) and load again - The data comes from the cache. If i change some value of the data in the kinvey backend, wait over the 100 seconds and start again, it just shows up the cached version.

i really can't see through it immo :neutral_face:





Maybe i'm just unterstand it wrong, but you said "offline always quering offline data" --- The docs say passing all 3 atributes (offline, fallback, refresh) resulting in exactly the result i'm aiming for.



So i thought this:

promise = Alloy.Globals.Collections.notes.fetch({

maxAge : 20,

//offline : true,

refresh : true,

fallback : true

});



is the "golden way" - First Request - everything is fine... and according to the kinvey debug informations it is saved correctly to the local storage. But at the second request, the app (ios) crashes and the simulator closes.









Best, Nico

I went through the library code, and it looks like the example above should do:

- Read data from cache.

- If maxAge exceeded, read from network. Save results from network in cache.



So, to answer your question, you’d need two calls: one to populate the cache (by omitting the offline flag), and then the example code above to implement the maxAge and refresh if exceeded.
Hey Mark,



still a important topic for me, but sadly i can't get it to work.





Just to be clear:

I want to make a request with maxAge of XXX. So the first time (there is no data) pull from the network and cache it.

After that if the request starts again (as a result of a user interaction) the data is pulled from the cache as long as the maxAge isn't reach. If it's expired it should pull from the network again.



Is this possible in a single call or do i have to write to calls and handle this by myself?



Cause you told me above setting offline:true always results in NOT pulling from network.

Regarding docs:

"The scenario below presents how data is retrieved from cache if not older than 1 hour (3600 seconds). If it is older than one hour, the item will be retrieved from the network. In that case, the item will also be put in cache with the 1 hour maxAge associated with it. "



var promise = Kinvey.DataStore.get('collection-name', 'entity-id', {

maxAge : 3600,

offline : true,

success : function(response) {

...

}

});





And that's the corresponding example - so i'm a little bit confused right now.





Best, Nico
That looks correct. Did you try reinstalling the entire app, so you start with a clean slate? Other than that, I am not sure what could be wrong.



Another thing you can try is setting KINVEY_DEBUG = true right before the init call. In the console window, a bunch of debug messages will be recorded. That should give you any clue if even an attempt is made to serve data from the local database.
var promise = Kinvey.init({

appKey : key_app,

appSecret : key_secret,

sync : {

enable : true,

online : Titanium.Network.getOnline() // The initial application state.

}

}).then( function(response){

....







That's my setup for Kinvey
I don’t see anything wrong with it right away. Note that if you pass `offline: true` it should never make a network request (not even one) to begin with. Did you populate the myEvents cache (i.e. execute the above request without `offline: true`)?



The behavior you are seeing could be caused by faulty initialization. Can you post your Kinvey.init call?
Login or Signup to post a comment