internal void RefreshLocalEntities(Action <int> setProgress) { var cloudTables = getAllKindNames(); //we get the keys and editdates foreach (var table in cloudTables) { var localCloudData = new CloudLocalStore <CloudEntity>(table.toKind()); var entities = localCloudData.GetUnsyncedLocalEntities( cloudTable: table.toKind(), localTable: getLocalTableName(table).toKind() ); if (entities.Count == 0) { continue; } var entityConverter = new EntityConverter(); var localStore = new CloudLocalStore <LocalEntity>(getLocalTableName(table).toKind()); var flatStore = new FieldValueStore(getTableFieldValueName(table)) { batchSize = 50 }; foreach (var entity in entities) { //we decrypt var localEntity = entityConverter.toLocalEntity(entity); localEntity.FormName = table; var saved = localStore.Update(localEntity); if (saved == null) { //means we couldn't save, so we do what? //throw exception?? log(string.Format("Couldn't save record for ", table, entity.Id)); continue; } //and deidentify var deid = entityConverter.toDeidEntity(localEntity); var ged = DbSaveableEntity.fromJson <GeneralEntityDataset>( new KindItem(deid.DataBlob) ); //todo: modify so we sync from CloudLocalStore separately than when downloading //and save to localTables flatStore.Save(ged, localEntity, saved.recordId); } //call finalise flatStore.finalise(); } //check if we have these in the local tables //fetch and process records missing //or do a sql comparison }
public async Task <int> doServerSync(Action <string, ToastLength> makeToast) { makeToast = (string a, ToastLength b) => { }; //var recs = GetRecordsToSync(); //if (recs.Count == 0) //{ // makeToast("No unsynced records", ToastLength.Short); // return 0; //} var hasConnection = await checkConnection(); if (!hasConnection) { isRunning = 0; makeToast?.Invoke("No connection detected", ToastLength.Long); return(0); } makeToast("Connected tested", ToastLength.Short); var recs = new List <OutEntity>(); //await GetCloudEntities1("appusers"); return(recs.Count); var cloudDb = new OutDb().DB; var recIndex = recs.Count - 1; while (recIndex >= 0 && hasConnection) { var outEntity = recs[recIndex]; var ppdataset = DbSaveableEntity.fromJson <GeneralEntityDataset>( new KindItem(outEntity.DataBlob)); var saveable = new DbSaveableEntity(ppdataset) { kindName = new KindName(ppdataset.FormName) }; var saved = false; for (int i = 0; i < 4; i++) { try { await Save(saveable); //we remove this key from the database saved = true; } catch (Google.GoogleApiException gex) { //todo: mark this record as bad to prevent it blocking for life //cloudDb.InsertOrReplace(new OutEntityUnsynced().load(outEntity)); //cloudDb.Delete<OutEntity>(saveable.Id.Value); //break; } catch (System.Net.WebException wex) { //perhaps lost connection //we alllow it to spin for now } catch (Exception ex) { //unknown exception } finally { } if (saved) { try { //we delete the record from downloads db //var deleted = cloudDb.Delete<OutEntity>(saveable.Id.Value); } catch { } break; } else { //lets add a 2 second delay in case it failed the first time await Task.Delay(TimeSpan.FromMilliseconds(2000)); } } recIndex--; hasConnection = await checkConnection(); } return(0); }
async Task <int> doServerSync(Action <string, ToastLength> makeToast) { makeToast("Getting records to sync", ToastLength.Short); var recs = GetRecordsToSync(); if (recs.Count == 0) { makeToast("No unsynced records", ToastLength.Short); return(0); } makeToast("Checking internet connection", ToastLength.Short); var hasConnection = await checkConnection(); if (!hasConnection) { isRunning = 0; makeToast("No connection detected", ToastLength.Long); //if (makeToast != null) //{ // makeToast("No connection detected", ToastLength.Long); //} return(0); } makeToast("Connection checked", ToastLength.Short); var cloudDb = new OutDb().DB; var totalCount = recs.Count; var recIndex = recs.Count - 1; while (recIndex >= 0 && hasConnection) { makeToast( string.Format("Processing {0} of {1}", totalCount - recIndex, totalCount) , ToastLength.Short); var outEntity = recs[recIndex]; var ppdataset = DbSaveableEntity.fromJson <GeneralEntityDataset>(new KindItem(outEntity.DataBlob)); var saveable = new DbSaveableEntity(ppdataset) { kindName = new KindName(ppdataset.FormName) }; var saved = false; var shouldCheckConnection = false; for (int i = 0; i < 4; i++) { makeToast( string.Format("Saving {0} of {1}" + (i == 0 ? "" : " (Attempt {2} of {3})"), totalCount - recIndex, totalCount, i + 1, 4) , ToastLength.Short); try { await Save(saveable); //we remove this key from the database saved = true; } catch (Google.GoogleApiException gex) { makeToast("Not saved - Google Api Exception", ToastLength.Short); //todo: mark this record as bad to prevent it blocking for life //cloudDb.InsertOrReplace(new OutEntityUnsynced().load(outEntity)); //cloudDb.Delete<OutEntity>(saveable.Id.Value); //break; } catch (System.Net.WebException wex) { makeToast("Not saved - Web Exception", ToastLength.Short); //perhaps lost connection //we alllow it to spin for now } catch (Exception ex) { makeToast("Not saved - Other Exception", ToastLength.Short); //unknown exception } finally { } if (saved) { try { var deleted = cloudDb.Delete <OutEntity>(saveable.Id.Value); } catch { } break; } else { //lets add a 2 second delay in case it failed the first time if (i < 3) { makeToast("Not saved. Awaiting for 2 secs", ToastLength.Short); await Task.Delay(TimeSpan.FromMilliseconds(2000)); } else { //we check the connection shouldCheckConnection = true; } } } recIndex--; if (recIndex >= 0 && shouldCheckConnection) { makeToast("Checking connection", ToastLength.Short); hasConnection = await checkConnection(); } } return(0); }