Android 애플리케이션에서 conflictHandler 클래스를 사용하여 모바일 장치의 DB 레코드와 서버 DB 간의 충돌을 감지합니다. 충돌이 감지되면 경고 대화 상자를 표시하여 사용자가 어떤 기록 버전이 서버 (또는 서버) 또는 "모바일 장치"에서 "이기게 될지"를 선택할 수 있습니다. 경고 대화 상자의 코드를 conflictHandler 클래스에 넣었습니다. 충돌이 감지되면 경고 대화 상자가 나타납니다. 문제는 경고 대화 상자가 나타나면 코드 실행이 멈추지 않아 사용자가 수행 할 작업을 선택할 수 있다는 것입니다. 항상 serverItem을 반환합니다.사용자가 경고 대화 상자에서 버튼을 선택할 때까지 실행 중지
private class ConflictResolvingSyncHandler implements MobileServiceSyncHandler {
@Override
public JsonObject executeTableOperation(
final RemoteTableOperationProcessor processor, final TableOperation operation)
throws MobileServiceSyncHandlerException {
final JsonObject clientItem = processor.getItem().getAsJsonObject();
MobileServicePreconditionFailedExceptionBase ex = null;
final JsonObject[] result = {null};
try {
result[0] = operation.accept(processor);
} catch (MobileServicePreconditionFailedExceptionBase e) {
ex = e;
} catch (Throwable e) {
ex = (MobileServicePreconditionFailedExceptionBase) e.getCause();
}
if (ex != null) {
// A conflict was detected; let's force the server to "win"
// by discarding the client version of the item
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Conflict Detected");
// set dialog message
final MobileServicePreconditionFailedExceptionBase finalEx = ex;
alertDialogBuilder
.setMessage("Choose winner")
.setCancelable(false)
.setPositiveButton("Server",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, Server wins
JsonObject serverItem = finalEx.getValue();
if (serverItem == null) {
// Item not returned in the exception, retrieving it from the server
try {
serverItem = mClient.getTable(operation.getTableName()).lookUp(operation.getItemId()).get();
} catch (Exception e) {
try {
throw new MobileServiceSyncHandlerException(e);
} catch (MobileServiceSyncHandlerException e1) {
e1.printStackTrace();
}
}
}
result[0] = serverItem;
}
})
.setNegativeButton("Client",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, Client wins
result[0]=clientItem;
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
return result[0];
}
@Override
public void onPushComplete(MobileServicePushCompletionResult result)
throws MobileServiceSyncHandlerException {
}
}
MobileServiceSyncHandler 코드를 넣을 수 있습니까? –
MobileServiceSyncHandler의 코드는 Windows Azure 모바일 서비스 패키지의 mobileservices-2.0.1-beta.jar에 있으며 불행히도 나는 단지 인터페이스 만 볼 수 있습니다. – Fivos