Answer the question
In order to leave comments, you need to log in
How to implement Retrofit 2 + Realm sync?
I have a SyncService class with a sync() method
public class SyncService {
//переменные
//конструктор
private void initRepos() {
//иницаилизация репозиториев
}
private void saveLastSyncTimeAsNow() {...}
private String getLastSyncTime() {...}
//здесь я получаю обьект с набором id по которым буду делать запросы (на каждый id должен быть отдельный запрос)
public void sync() {
String lastSyncTime = getLastSyncTime();
Observable<Sync> syncObservable = mRestClient.getSyncObjectObservable(lastSyncTime);
syncObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Sync>() {
@Override
public void onNext(Sync syncObject) {
//обновление базы в 3 этапа
mDatabaseRepository.removeRealmObjects(syncObject.getRemoves());
updateDB(syncObject.getUpdates());
initAddedRefs(syncObject.getAddedRefs());
}
});
}
private void saveResponce(Response response){
if (rlm == null) {
rlm = mDatabaseRepository.getRealm();
}
rlm.beginTransaction();
rlm.copyToRealmOrUpdate((RealmObject) response.body());
rlm.commitTransaction();
}
public void updateDB(List<Updates> updates) {
for (Updates update : updates) {
updatesSize++;
switch (update.getType()) {
case Constants.OBJECTS_TYPES.BRANCH:
for (int n = 0; n < update.getIds().size(); n++) {
Call<Branch> branchCall = mApiService.getBranch(update.getIds().get(n));
branchCall.enqueue(new Callback<Branch>() {
@Override
public void onResponse(Call<Branch> call, Response<Branch> response) {
if (response.isSuccessful()){
saveResponce(response);
}
}
@Override
public void onFailure(Call<Branch> call, Throwable t) {
}
});
}
break;
case Constants.OBJECTS_TYPES.PRODUCT:
for (int n = 0; n < update.getIds().size(); n++) {
Call<Product> productCall = mApiService.getProduct(update.getIds().get(n));
productCall.enqueue(new Callback<Product>() {
@Override
public void onResponse(Call<Product> call, Response<Product> response) {
if (response.isSuccessful()){
saveResponce(response);
}
}
@Override
public void onFailure(Call<Product> call, Throwable t) {
}
});
}
break;
case Constants.OBJECTS_TYPES.PROJECT:
for (int n = 0; n < update.getIds().size(); n++) {
Call<Project> projectCall = mApiService.getProjectObservable(update.getIds().get(n));
projectCall.enqueue(new Callback<Project>() {
@Override
public void onResponse(Call<Project> call, Response<Project> response) {
if (response.isSuccessful()){
saveResponce(response);
}
}
@Override
public void onFailure(Call<Project> call, Throwable t) {
}
});
}
break;
}
}
}
public void initAddedRefs(List<AddedRefs> addedRefs) {
for (AddedRefs addedRef : addedRefs) {
int entityRefference = addedRef.getEntityReferenceType();
switch (entityRefference) {
case Constants.REFFERENCE_TYPES.RELATIVE:
switch (addedRef.getFirstType()){
case Constants.OBJECTS_TYPES.USER:
//TODO userRepo or so
break;
case Constants.OBJECTS_TYPES.PRODUCT:
mProductRepository.getProductByLocalId(addedRef.getFirstId(), new OnGetProductByLocalIdCallback() {
@Override
public void onSuccess(Product product) {
mProjectRepository.getProjectByLocalId(addedRef.getSecondId(), new OnGetProjectByLocalIdCallback() {
@Override
public void onSuccess(Project project) {
mProductRepository.addProjectToProduct(product, project, null);
}
});
}
});
break;
case Constants.OBJECTS_TYPES.PROJECT:
mProjectRepository.getProjectByLocalId(addedRef.getFirstId(), new OnGetProjectByLocalIdCallback() {
@Override
public void onSuccess(Project project) {
switch (addedRef.getSecondType()){
case Constants.OBJECTS_TYPES.TASK:
mTaskRepository.getTaskByServerId(addedRef.getSecondId(), new OnGetTaskByLocalIdCallback() {
@Override
public void onSuccess(Task result) {
mProjectRepository.addTaskToProject(project, result, null);
}
});
break;
case Constants.OBJECTS_TYPES.ITERATION:
mIterationRepository.getIterationByServerId(addedRef.getSecondId(), new OnGetIterationByLocalIdCallback() {
@Override
public void onSuccess(Iteration result) {
mProjectRepository.addIterationToProject(project, result, null);
}
});
break;
case Constants.OBJECTS_TYPES.COMMENT:
mCommentRepository.getCommentByLocalId(addedRef.getSecondId(), new OnGetCommentByLocalIdCallback() {
@Override
public void onSuccess(Comment result) {
mProjectRepository.addCommentToProject(project, result, null);
}
});
break;
case Constants.OBJECTS_TYPES.BUILD:
//TODO buildRepo
break;
case Constants.OBJECTS_TYPES.SPECIFICATION:
//TODO SpecRepo
break;
}
}
});
break;
}
}
}
mDatabaseRepository.removeRealmObjects(syncObject.getRemoves());
updateDB(syncObject.getUpdates());
void initAddedRefs(List<AddedRefs> addedRefs)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question