Skip to content

Commit 2f65a66

Browse files
authored
Merge pull request #1138 from NativeScript/trifonov/improve-livesync-wait
improve livesync wait
2 parents 76f9b76 + 80a3cc6 commit 2f65a66

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

test-app/app/src/main/java/com/tns/RuntimeHelper.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,7 @@ public static Runtime initRuntime(Application app) {
194194
// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
195195
initLiveSync(runtime, logger, app);
196196

197-
// CLI will create this file when initial sync is needed and then will remove it after syncing the fails and restarting the app
198-
File liveSyncFile = new File("/data/local/tmp/" + app.getPackageName() + "-livesync-in-progress");
199-
if(liveSyncFile.exists()) {
200-
try {
201-
// wait for the livesync to complete
202-
Thread.sleep(30000);
203-
} catch (Exception ex) {
204-
205-
}
206-
}
197+
waitForLiveSync(app);
207198
}
208199

209200
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));
@@ -239,6 +230,32 @@ public static Runtime initRuntime(Application app) {
239230
}
240231
}
241232

233+
private static void waitForLiveSync(Application app) {
234+
boolean needToWait = false;
235+
236+
// CLI will create this file when initial sync is needed and then will remove it after syncing the fails and restarting the app
237+
File liveSyncFile = new File("/data/local/tmp/" + app.getPackageName() + "-livesync-in-progress");
238+
if(liveSyncFile.exists()) {
239+
needToWait = true;
240+
Long lastModified = liveSyncFile.lastModified();
241+
// we check for lastModified == 0 as this might happen if we cannot get the actual modified date
242+
if(lastModified > 0) {
243+
Long fileCreatedBeforeMillis = System.currentTimeMillis() - lastModified;
244+
// if last modified date is more than a minute before the current time discard the file as most probably this is a leftover
245+
if (fileCreatedBeforeMillis > 60000) {
246+
needToWait = false;
247+
}
248+
}
249+
}
250+
251+
if(needToWait) {
252+
try {
253+
// wait for the livesync to complete and it should restart the app after deleting the livesync-in-progress file
254+
Thread.sleep(30000);
255+
} catch (Exception ex) { }
256+
}
257+
}
258+
242259
private static void registerTimezoneChangedListener(Context context, final Runtime runtime) {
243260
IntentFilter timezoneFilter = new IntentFilter(Intent.ACTION_TIMEZONE_CHANGED);
244261

0 commit comments

Comments
 (0)