我有一个与azure sql数据库通信的Android应用程序。我正在实现脱机同步,但在第一次启动应用程序后,本地表不会与数据库同步,反之亦然。只有我第一次拉到当地的桌子。
private AsyncTask<Void, Void, Void> initLocalStore() throws MobileServiceLocalStoreException, ExecutionException, InterruptedException {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
MobileServiceSyncContext syncContext = mClient.getSyncContext();
if (syncContext.isInitialized())
return null;
SQLiteLocalStore localStore = new SQLiteLocalStore(mClient.getContext(), "DB_ONYAX", null, 1); //DB_ONYAX = database name
//Level_Onyax_Structures table definition
Map<String, ColumnDataType> tableDefinitionLOS = new HashMap<String, ColumnDataType>();
tableDefinitionLOS.put("id", ColumnDataType.String);
tableDefinitionLOS.put("updatedAt", ColumnDataType.DateTimeOffset);
tableDefinitionLOS.put("version", ColumnDataType.String);
tableDefinitionLOS.put("ID_ONYAX_STRUCTURE", ColumnDataType.Integer);
tableDefinitionLOS.put("Structure_name", ColumnDataType.String);
tableDefinitionLOS.put("Structure_LAT", ColumnDataType.Real);
tableDefinitionLOS.put("Structure_LON", ColumnDataType.Real);
tableDefinitionLOS.put("Structure_ZOOM", ColumnDataType.Real);
localStore.defineTable("Level_Onyax_Structures", tableDefinitionLOS);
SimpleSyncHandler handler = new SimpleSyncHandler();
syncContext.initialize(localStore, handler).get();
} catch (final Exception e) {
createAndShowDialogFromTask(e, "Error Creation Local Table");
}
return null;
}
};
return runAsyncTask(task);
}
刷新移动服务中的项目:
private List<My_Table> refreshItemsFromMobileServiceTableSyncTable() throws ExecutionException, InterruptedException {
//sync the data
syncLOS().get();
return mLOSTable.read(null).get();
}
同步方法:
private AsyncTask<Void, Void, Void> syncLOS() {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
MobileServiceSyncContext syncContext = mClient.getSyncContext();
syncContext.push().get();
mLOSTable.pull(null).get();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
};
return runAsyncTask(task);
}
在这里,我称之为一切:
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
listOnyaxStructures = refreshItemsFromMobileServiceTableSyncTable();
runOnUiThread(new Runnable() {
@Override
public void run() {
setLoading(false);
}
}
});
} catch (final Exception e){
e.printStackTrace();
}
return null;
}
};
runAsyncTask(task);
我尝试更新值的地方:
item.setStructure_name(edt.getText().toString());
new AsyncTask<Void, Void, Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
mLCSTable.update(item).get();
MobileServiceSyncContext syncContext = mClient.getSyncContext();
syncContext.push().get();
mLCSTable.pull.get();
runOnUiThread(new Runnable() {
@Override
public void run() {
//finish
}
});
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
有帮助吗?
答案 0 :(得分:0)
更新:我找到了MobileServicePushFailedException的原因。我得到MobileServicePushFailedException,因为当我尝试更新项目时,我正在尝试更新自动增量的索引。
这是Easy Tables日志中的错误:无法更新标识列'ID_COMPANY_STRUCTURE'。
我已经尝试过不将ID_COMPANY_STRUCTURE插入到本地表中并且一切正常,但是我需要它,因为它是另一个表的外键。
有没有办法在Android上设置IDENEN关闭或避免这种类型的错误?