Xamarin.Forms - Akavache how to recreate Sqlite3 connection after BlobCache.Shutdown() - xamarin

I want the user to be able to leave the App and to come back as many times as he wants. Saving its data in the cache each time the app is sent background.
When my App on Xamarin.Forms goes "OnSleep()" the following code is performed:
BlobCache.LocalMachine.Flush().Wait();
await BlobCache.LocalMachine.InsertObject("test",value);
BlobCache.Shutdown().Wait();
The first time, it works.
But when the app is called to the front and then is resent to background, then it failed.
I notice, "BlobCache.Shutdown().Wait();" kills the BlobCache.LocalMachine's connection to Sqlite3.
And when the App is back to the front, this connection is not recreated.
Do you know, how to recreate this connection ?
Best regards

Related

NativeScript Hybrid Mobile App: Tracking/Capturing Crashes

I am building a NativeScript mobile app and among other things I am capturing for analytics purposes, I need to capture "app crashes" possibly with errors/reasons it crashed.
I came across this SO post but there it was in the response of a question on how not to let the app crash. Following was suggested to catch crash events:
var application = require("application");
application.on(application.uncaughtErrorEvent, function (args) {
if (args.android) {
// For Android applications, args.android is an NativeScriptError.
console.log("NativeScriptError: " + args.android);
} else if (args.ios) {
// For iOS applications, args.ios is NativeScriptError.
console.log("NativeScriptError: " + args.ios);
}
});
If I go by the above then I have the following questions. Would appreciate if somebody can confirm if this means every-time the app is crashing it will generate this application.uncaughtErrorEvent event? Can I rely on it?
If it is true then maybe I can make a REST call to my backend and store date, time and whatever is in args.android or args.ios.
If above is not the correct way then can somebody please help me on how to go about doing this?
Any help is highly appreciated. Thank you!
The application.onUncaughtError will be hit probably 95-98% of the time during a crash; it is pretty reliable. I have seen the app crash without any notice at all it just goes poof, but I'm not sure any reporting system can handle that one.
The way I do it is during the app startup I register a couple things:
I create a global.error function; this is used for anything (like try/catch, promise/catch) that needs to send the errors through to be logged remotely. So anywhere in my codebase I can do a global.error(theError); and it will be handled; this way I do NOT have to worry about trying to load or require things while an error is taking place, as that can cause other errors.
I use the onUncaughtError event to catch anything that is not normally caught, and then notify the user that an error has occurred and quit the app. (in this case, trying to recover is not recommended as you have no idea where the error was thrown from...)
If I use a worker thread, When I startup a worker I register the worker.onerror to forward its data to the main threads global.error function AND I have a specific message from the workers' version of the global.error that sends the error back to the primary thread. This way if the worker itself calls global.error that message is passed back to the main thread and then to the global.error on the main thread which handles everything properly.
This technique allows me to catch pretty much all errors that can occur. The main global.error function and the onUncaughtError both use a simple reporting library that I built that reports all the data back to one of my servers, IF the device is online. If the device is offline, it can optionally save the data to a reporting file to be uploaded later; or just ignore it.
It also has safety checks to verify the error isn't a network error (we don't want the error reporting to go into a loop, i.e. trying to report the error causes an error, which then tries to report the error; so in the event it is a certain type of network error; it will ignore those. )

"New version available" with service worker and sw-precache

I'm trying to use sw-precache, but I must be doing something wrong!
I'm mostly using the demo code available from the github repo and can't seem to get updates to the app to come through. Once it's cached the first time, it never checks for new versions.
I was expecting that when I publish a new service worker, the browser would request the new service worker and update the cache accordingly in the background. Then using the registration code in the example, I would be able to prompt the user to refresh and get the latest version from their newly refreshed cache.
Would really appreciate if someone could please point me in the right direction.
Example
To demonstrate the problem, I've created an isolated example here:
https://github.com/stevenocchipinti/sw-precache-demo
The example uses a basic skeleton from create-react-app which has a built in build task which take care of fingerprinting the filenames, etc.
I suspect the problem is with me caching everything by using the following sw-precache config:
{
"staticFileGlobs": [ "build/**/*.*" ],
"stripPrefix": "build/"
}
There are more accurate steps in the repo's readme, but the basic steps I'm taking to reproduce the problem are as follows (with my probably incorrect expectations).
Steps and Assumptions
Browse to the app for the first
I should see Content is now available offline! in the console
Reload the page
The message in the console should not appear again because the service worker is installed, but the page should still work.
Go offline and reload the page
The page should still work
Make a visible change to the source code
Rebuild (run the build task and sw-precache)
This is where my understanding must be wrong
Reload the page
The service worker should update the cache in the background
When its done, you should see New or updated content is available. in the console
The actual visible changes should not be visible until the next reload
Reload the page again
The browser will use the new cache this time around
The changes should be visible now!
There shouldn't be any messages in the console
The problem
Once the app has been cached initially, it will never update unless you unregister the service worker or force a reload.
I'm not sure how to make this work - any help would be greatly appreciated!
After replicating your development hosting environment, I can see that you're serving your service-worker.js file with a browser HTTP cache lifetime of one hour:
There's more information as to why this is leading to the behavior you're seeing, along with best practices, in this previous answer. As mentioned at the top of that answer, browsers plan on changing their behavior to stop honoring the HTTP cache for the service worker file by default, mainly due to the type of confusion that you're experiencing here. For the time being, though, the production versions of both Chrome and Firefox continue to honor those headers.

Laravel 5.1 randomly dropping session data

I have a strange issue with a Laravel 5.1 application.
Intermittently, it’s dropping session data. I’m detected this by writing some middleware that writes the contents of the session for that request to the log file. Although the session ID (Session::getId()) doesn’t change, the value of _token in the session data retrieved with Session::all() does.
As I say, this happens intermittently. I can refresh the same URL multiple times, and then randomly on one refresh the session data’s gone, and the _token value’s different from the previous requests.
What would cause this? I’ve also noticed the flash object isn’t in the “dropped” session data.
Below is a snippet of the log. You can see the content of the session_data key randomly changes “shape” in the last two lines, but the session ID remains constant.
Also, not sure if it’s pertinent, but I have DebugBar enabled.
UPDATE: Through debugging, I’ve found that on some page loads the session is completely empty, as in, no _token (hence a new one getting generated). Nothing.
If you're using the file driver, you could run into race conditions on concurrent requests. The file then gets truncated, Laravel can't read it, so it refreshes the session. Race conditions can also lead to a symptom where something you're putting to the session just doesn't get put. This tends to be random, so it's very hard to debug. According to the Laravel team, this is a known limitation of the file driver and it does not appear to be getting fixed, so I would suggest using a different driver. This would fix your issue of random session refreshes, but it still introduces a possibility of making a change to the session that doesn't get added. As far as I know, at this point with Laravel 5.1, you'll have to manage that yourself.
Somehow your session data is too long and being truncated. If you're using the database driver (haven't tested other drivers), and you try to save session data that's longer than the field length, then subsequent requests won't be able to pull from this session, and you'll wind up with a new session. If this issue is happening randomly with very short session data, then it's probably the cause listed above.
If you use Linux, Try using Redis (http://redis.io) as session / cache manager in laravel. I had some issues in the past with text / cookies and laravel in some servers. When I instaled Redis I had no problems anymore.
More info: https://laravel.com/docs/5.1/redis
Using a different driver like memcached did not solve the problem for me.
Here is a package that implements session locking which works and very simple to incorporate in your projects.
https://github.com/rairlie/laravel-locking-session

iOS8: NSURLSession: NSURLSessionDataTask 'Client closed connection before receiving entire response'

I use iOS BACKGROUND FETCH n BACKGROUND TRANSFER to update our app.
When we download an MP4 as part of a large download of lots of files the download can stop suddenly and get error:
'Client closed connection before receiving entire response'
http code is still 200.
We have no limit on data due to corporate cellular accounts and have single apps on our iPad so management want us to download a larger amount of data and number of files sometime over 20/30 on a clean install.
They're corporate videos/ pdfs published daily.
I created a NSURLSession
NSURLSession * backgroundSession_ = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier]
Then for each URL to the mp4 or PDF I create a NSURLSessionDownloadTask from my NSURLSession
NSURLSessionDownloadTask * downloadTask_ = [backgroundSession_ downloadTaskWithURL:url_];
The fire off resume on each download task.
When all return the NSURLSession completes and I tell the user in a notification that the apps has been updated.
I use CHARLES PROXY on my mac to monitor the downloads.
It takes a while as there are many files and also I noticed the movies seem to stream so can be slow.
Usually it checks whats been downloaded and only downloads the latest but if I do a lot of downloads say to a clean install I noticed that quiet often the streams to the mp4 get killed.
They have a 200 code but with message
'Client closed connection before receiving entire response'
Any idea what may cause this because it can stop my NSURLSession from completing so my sync can get in a sort of hung state.
IMAGE: OK DOWNLOAD: MOVIE STREAMS AND SAVED TO DISK: 200: Complete
IMAGE: DOWNLOAD SUDDENLY STOPS:
Any ideas: I use Background Fetch so not sure if the client in the message is iOS8 or my app.
After each downloadtask returns I copy the tmp file to mp4 in documents and call
[session finishTasksAndInvalidate];
if some of these downloadtask fail then I notice the session never finishes and gets stuck.
When I do another fetch I get all these old download task and old sessions.
was asked how I solved this but two years laster.
We also shrunk the movie files as well to speed up downloads.
I also remember the session and the configuration having separate settings. I had set header in one but failed when iOS 8/9 came out as it should have been set in the session not just the config - but may be fixed now as iOS 10 out
//-----------------------------------------------------------------------------------
//some movie streams time out n get
//'http://stackoverflow.com/questions/23428793/nsurlsession-how-to-increase-time-out-for-url-requests'
//'Client closed connection before receiving entire response'
NSLog(#"urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest:%f", urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest);
NSLog(#"urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource:%f", urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource);
//was 60
//urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest = 240.0;
urlSessionConfigurationBACKGROUND_.timeoutIntervalForRequest = 600.0;
// urlSessionConfigurationBACKGROUND_.timeoutIntervalForResource = 60.0;
You can ask your server mate to change the content-type to video/mpeg4 or you can change your request type from GET to POST

Android Webview - can't disable cache

I'm using webview in my app which is loading remote web page, which is then using socket.io (node.js) via xhr-pooling.
Problem is that I can't disable caching of received data through socket.io.
For example, every 10 seconds my node server does io.emit, and my webview receives it and saves it in:
/data/data/...../webviewCache
I do not want my webview anything to save, because as time passes number of those files is just rising and they aren't helping my app run faster...
I've tried:
browser.getSettings().setCacheMode(2); //(2 is LOAD_NO_CACHE)
browser.getSettings().setAppCacheEnabled(false);
but neither of those works. My webview is still saving files to the cache folder.
At this moment, I've set up timer which is emptying cache folder every 60 seconds but that's not solution I would like to release in production...
Am I missing something here or there is bug with disabling cache within android?
UPDATE 1: After whole day of debugging I've found out something interesting.
Logcat shows two interesting things: saveCacheFile and getCacheFile
Then I've decided once again to try turn off the cache...
browser.getSettings().setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);
That actually caused that WebView wasn't loading files from cache anymore, but it was still saving them. Log cat says something like this:
saveCacheFile for url .../socket.io/1/xhr-polling/BLNN28E7S4PZJsy2pWaF?t=13537
So I believe actual question would be, how to prevent webview from SAVING cache files on every request.
How about adding random string in the query part of your URL? This trick works under some cases.
The only solution I found was to send "Cache-Control: no-store" in the HTTP response header.

Resources