I'm using Parse Server for my Android app and everything is working fine, but every time I call saveEventually on a new or old ParseObject, it is taking a really long time. Sometimes it's more than 1 minute for 1 item to return the callback.
Anyone had this problem?
Example:
orderObject.p.apply {
put(ORDER_STATE, ORDER_STATE_FINISHED)
put(ORDER_NEXT_DATE, orderEndDate)
}
createLog("FinishOrderSeq", "OrderActivity - saveOrder - before saveEvent")
orderObject.p.saveEventuallyEx(isOnline(this)){ e ->
createLog("FinishOrderSeq", "OrderActivity - saveOrder - after saveEvent")
if (e == null){
createToast(getString(R.string.order_dialog_success), this)
createOrderCopy(orderObject, dialog)
} else {
createToast(getString(R.string.order_dialog_err), this)
changeButtonState(posBtn, true)
changeButtonState(negBtn, true)
}
}
fun ParseObject.saveEventuallyEx(isOnline: Boolean, callback: (ParseException?) -> Unit){
if (isOnline){
saveEventually{ err ->
callback(err)
}
} else {
saveEventually()
callback(null)
}
}
Also logs as I replaced it with saveInBackground with callback(still 30 seconds):
2020-05-28 14:53:49.805 18673-18673/? I/FinishOrderSeq: OrderActivity - saveOrder - before saveEvent
2020-05-28 14:54:15.694 18673-18673/? I/FinishOrderSeq: OrderActivity - saveOrder - after saveEvent
UPDATE:
So I figured out from parse dashboard, that ParseObject is saved as record in table immediatelly, but callback from saveEventually is sent after 30sec - 2 minutes.
UPDATE 2:
I also tried to use saveInBackground() if user is online (with callback). This also took 30seconds to 2 minutes for callback to return. Object was saved to parse database with all data after 100ms (checked from Parse Dashboard).
Then I thought something is wrong with ParseSDK threads, so I used save() inside Coroutine. Same problem occured here, save() took up to 2 minutes to perform.
Code with coroutine:
fun ParseObject.saveAsync(context: CoroutineContext, scope: CoroutineScope, isOnline: Boolean, callback: (ParseException?) -> Unit){
if (isOnline){
scope.launch {
var ex: ParseException? = null
try {
save()
} catch (e: ParseException){
ex = e
}
withContext(context){
callback(ex)
}
}
}
}
There is some serious problem with callbacks in ParseSDK for Android and I don't know what can cause this. No exception no error on server side.
UPDATE 3:
After deeper investigation, I found which function is taking long time to proceed.
ParseObject.State result = saveTask.getResult();
Approximately 30 seconds - 2 minutes to get into next line of code.
This is lowest level of function I can get inside SDK.
Inside function save() or saveInBackground() there is this inner function in Java:
Task<Void> saveAsync(final String sessionToken, final Task<Void> toAwait) {
if (!isDirty()) {
return Task.forResult(null);
}
final ParseOperationSet operations;
synchronized (mutex) {
updateBeforeSave();
validateSave();
operations = startSave();
}
Task<Void> task;
synchronized (mutex) {
// Recursively save children
/*
* TODO(klimt): Why is this estimatedData and not... I mean, what if a child is
* removed after save is called, but before the unresolved user gets resolved? It
* won't get saved.
*/
task = deepSaveAsync(estimatedData, sessionToken);
}
return task.onSuccessTask(
TaskQueue.<Void>waitFor(toAwait)
).onSuccessTask(new Continuation<Void, Task<ParseObject.State>>() {
#Override
public Task<ParseObject.State> then(Task<Void> task) {
final Map<String, ParseObject> fetchedObjects = collectFetchedObjects();
ParseDecoder decoder = new KnownParseObjectDecoder(fetchedObjects);
return getObjectController().saveAsync(getState(), operations, sessionToken, decoder);
}
}).continueWithTask(new Continuation<ParseObject.State, Task<Void>>() {
#Override
public Task<Void> then(final Task<ParseObject.State> saveTask) {
ParseObject.State result = saveTask.getResult(); <--- THIS IS TAKING LONG TIME
return handleSaveResultAsync(result, operations).continueWithTask(new Continuation<Void, Task<Void>>() {
#Override
public Task<Void> then(Task<Void> task) {
if (task.isFaulted() || task.isCancelled()) {
return task;
}
// We still want to propagate saveTask errors
return saveTask.makeVoid();
}
});
}
});
}
From the docs:
Most save functions execute immediately, and inform your app when the save is complete. If you don’t need to know when the save has finished, you can use saveEventually instead.
It can take a long time because with saveEventually you are basically saying "save it soon". If you want to "save it as soon a possible" then use saveInBackground as described in the docs.
Further it says:
All calls to saveEventually (and deleteEventually) are executed in the order they are called, so it is safe to call saveEventually on an object multiple times. If you have the local datastore enabled, then any object you saveEventually will be pinned as long as that save is in progress. That makes it easy to retrieve your local changes while waiting for the network to be available.
Which means that you can save and modify the object locally multiple times and the latest version will be stored in the database as soon as the network connection is reestablished.
Related
I am developing an Android app.
RxJava is used.
It stores user data in the local database with expire time.
First, it gets user data from the local database.
And check the expire time, if the data is old data, then it gets user data from remote server and update it to the local database.
fun getPlayer(playerId: String): Single<Player> {
return playerDao.getPlayer(playerId)
.doOnSuccess { // "doOnSuccess" is right? what method should I use?
if (PlayerUtil.isNeededUpdate(it)) {
Log.d(TAG, "getPlayer(local) - old!")
getPlayerFromRemote(playerId)
// How can I return Observable/Flowable/Single in here?
// (case of need to update data)
}
}
.onErrorResumeNext {
// If local database has no player, then it try to get it from remote server
Log.d(TAG, "getPlayer(local) - onError: ${it.message}")
getPlayerFromRemote(playerId)
}
}
doOnSuccess is meant to be used for side-effects, not actions that impact the stream itself.
What you're looking for is flatMap and just returning the scalar value if nothing needs to be done:
.flatMap {
if (PlayerUtil.isNeededUpdate(it)) {
getPlayerFromRemote(playerId)
} else {
Single.just(it)
}
}
I'm trying to use RxJava with Android to asynchronously update my view. When user clicks the movie from the list in the RecyclerView, I want to present him first with the movie from the database, if present. Then I want to fetch the latest information and update the database as well as UI. I'm trying to use concat method and its variant but it does not work.
I have skipped other codes only to post the relevant RxJava methods that are fetching data as the rest is working fine.
When I disable network connection with the code below (hence remote returns error), the code below does not display data from the database at all. Only it reports the error. Which means the local is not resolving.
public Flowable<Movie> getMovie(final int id) {
return Single.concat(mLocal.getMovie(id), mRemote.getMovie(id).doOnSuccess(data -> {
mLocal.save(data);
})).onErrorResumeNext(error->{
return Flowable.error(error);
});
}
And in this code, it works fine, except now that I don't get the error message (and rightly so, since I have replaced it with new stream from the database)
public Flowable<Movie> getMovie(final int id) {
return Single.concat(mLocal.getMovie(id), mRemote.getMovie(id).doOnSuccess(data -> {
mLocal.save(data);
})).onErrorResumeNext(error->{
return mLocal.getMovie(id).toFlowable();
});
}
Now, how can I get database data first and then fire network call next to update data and get errors from the database or network call?
UPDATE
The latest method code
// calling getMovie on mLocal or mRemote returns Single
public Flowable<Movie> getMovie(final int id) {
return Single.concat(mLocal.getMovie(id), mRemote.getMovie(id).doOnSuccess(data -> {
mLocal.insertMovie(data);
})).onErrorResumeNext(error -> {
return Flowable.error(error);
});
}
Here is how I call them
public void loadMovie(int id)
{
Disposable d = mRepo.getMovie(id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread(), true)
.doOnSubscribe(subscription -> {
subscription.request(Long.MAX_VALUE);
//post progress here
})
.subscribe(data -> {
//onNext
},
error -> {
//onError
},
() -> {
//onComplete
}
);
mDisposables.add(d);
}
With affirmation that my code works and guides on troubleshooting from #akarnokd I found the latest code (see OP) works flawlessly. The result of RxJava chain is posted to LiveData object which should update View. Unfortunately it only posts the latest data (which is an error) and skips the first (which is the data from the database).
I will deal with that but since the post deals with RxJava, I will consider this solved!
I am developing a splash screen for an Android app which has a few requirements:
While the splash screen is showing I need to fetch a value from a
repository. Once that value is fetched it will be used to kick off 3
different calls to the repository in parallel.
The splash screen
must show for at least 3 seconds or as long as it takes to fetch all
values from the repository (if the fetches take longer than 3
seconds).
If an error occurs when fetching the first value from
the repository then the observable chain can terminate (once 3
seconds have elapsed)
If any errors occur when fetching the next
three values the observable chain should not terminate, that is, all
three should run even if one or more fail.
Here's what I have currently:
Observable<Long> timerObservable = Observable.timer(3, TimeUnit.SECONDS);
cachedObservable = mLoginRepository
.fetchLoginPreference()
.onErrorReturn(new Func1<Throwable, LoginPreference>() {
#Override
public LoginPreference call(Throwable throwable) {
return null;
}
})
.flatMap(new Func1<LoginPreference, Observable<CompositeLPLCPalette>>() {
#Override
public Observable<CompositeLPLCPalette> call(final LoginPreference loginPreference) {
if (loginPreference == null
|| loginPreference.getActivationCode() == null
|| loginPreference.getActivationCode().isEmpty()) {
Timber.d("login preference was null");
return Observable.just(null);
}
final String activationCode = loginPreference.getActivationCode();
Observable<LoginCapability> loginCapabilityObservable = mLoginRepository
.fetchLoginCapability(activationCode, true)
.onErrorReturn(new Func1<Throwable, LoginCapability>() {
#Override
public LoginCapability call(Throwable throwable) {
return null;
}
});
Observable<OrgContactInfo> orgContactInfoObservable = mLoginRepository
.fetchOrgContactInfo(activationCode, true)
.onErrorReturn(new Func1<Throwable, OrgContactInfo>() {
#Override
public OrgContactInfo call(Throwable throwable) {
Timber.d("Error fetching org contact info");
return null;
}
});
Observable<Palette> paletteObservable = mLoginRepository
.fetchThemeInformation(activationCode, true)
.onErrorReturn(new Func1<Throwable, Palette>() {
#Override
public Palette call(Throwable throwable) {
Timber.d("Error fetching Palette");
return null;
}
});
return Observable.zip(loginCapabilityObservable,
paletteObservable,
orgContactInfoObservable,
new Func3<LoginCapability, Palette, OrgContactInfo, CompositeLPLCPalette>() {
#Override
public CompositeLPLCPalette call(LoginCapability loginCapability, Palette palette, OrgContactInfo orgContactInfo) {
return new CompositeLPLCPalette(loginCapability, loginPreference, palette);
}
});
}
})
.zipWith(timerObservable, new Func2<CompositeLPLCPalette, Long, CompositeLPLCPalette>() {
#Override
public CompositeLPLCPalette call(CompositeLPLCPalette compositeLPLCPalette, Long aLong) {
return compositeLPLCPalette;
}
});
The code above works but I have a few questions:
1) Is the way I'm enforcing the 3 second minimum the correct way to do it? It looked like there was a delay operator as well as the timer operator and it wasn't clear which I should use. Also, should I be zipping the timer operator with the rest of the chain?
2) Am I using onErrorReturn() correctly if my intention is that if the observable fails it should just return null instead of having the subscriber's onError() method?
3) In the flatMap() operator I'm checking to see if loginPreference is null, has a null activation code or empty activation code and if any of those things are true I don't want to run the other 3 observables. Is there a different operator I should be using before the flatMap() operator instead of adding this logic to flatMap()?
Yep, that's the correct way to do it. I would suggest extracting the contents of the flatMap as a separate method, and keep the 2 zip operators separate for logic and maintenance reasons.
I really don't like having nulls in observables, at a minimum it's incompatible with RxJava 2. However given the requirements the logic feels sound.
Nah, you're good.
I am new to Rxjava and exploring the possibilities and need help in below described scenario.
Step 1 : Swipe the View
Step 2 : Make an API Call to fetch Data
The above steps are repeated for the number of Views.
Problem :
API_CALL_1 fetching the View_1 Results
User swipes a View_2
API_CALL_2 fetching the View_2 Results
View_1 results are returned and populated in View_2 along with View_2 results
I need to cancel the API_CALL_1 request when the API_CALL_2 request.
Thanks.
Class member:
Subscription mSubscription;
Your API call:
if(subscription != null && !subscription.isUnsubscribed()){
subscription.unsubscribe();
subscription = null;
}
subscription = doApiCall("some_id")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Object>() {
#Override
public void call(Object o) {
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
}
});
So, the main idea of it, that you have to unsubscribe from previous call:
if(subscription != null && !subscription.isUnsubscribed()){
subscription.unsubscribe();
subscription = null;
}
Before starting new call
a better way is to use switchMap to cancel previous request
You are able to provide 'cancel' logic by using 'switchMap' operator. For example, you want to start/stop loading (Code written in Kotlin).
val loadingObservable = Observable.timer(10, TimeUnit.SECONDS)
val load = PublishSubject.create<Boolean>()
val resultObservable = load.switchMap { if(it) loadingObservable else Observable.never() }
Explanation:
'loadingObservable' is a timer that simulate long running operation
'load' is a command from user to start or stop loading. If you want
to start loading - send True else False
'switchMap' operator is
designed to dispose previous observable. So it you send True it will
start loading. If you send False it will dispose (cancel) previous
observable.
I have an API call and I want to wrap it using Observable:
private Observable<RealmResults<Account>> getAccounts() {
final Observable<RealmResults<Account>> realmAccounts =
Observable.defer(new Func0<Observable<RealmResults<Account>>>() {
#Override
public Observable<RealmResults<Account>> call() {
return RealmObservable.results(getActivity(), new Func1<Realm, RealmResults<Account>>() {
#Override
public RealmResults<Account> call(Realm realm) {
return realm.where(Account.class).findAll();
}
});
}
});
return Observable
.create(new Observable.OnSubscribe<RealmResults<Account>>() {
#Override
public void call(final Subscriber<? super RealmResults<Account>> subscriber) {
DataBridge.getAccounts(Preferences.getString(Constant.ME_GUID, ""), new OnResponseListener() {
#Override
public void OnSuccess(Object data) {
Log.d("Stream", "onSuccess");
realmAccounts.subscribe(subscriber);
}
#Override
public void onFailure(Object data) {
subscriber.onError(new Exception(data.toString()));
}
});
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.startWith(realmAccounts);
}
and I use it like
Observable<Accounts> accounts = getAccounts().flatMap(
new Func1<RealmResults<Account>, Observable<Account>>() {
#Override
public Observable<Account> call(RealmResults<Account> accounts) {
return Observable.from(accounts);
}
});
How can I use the accounts observable multiple times without calling the API each time. I need to process the stream of accounts and extract different sets of data out of it.
The easiest method is to use operator cache, which internally uses ReplaySubject. It cache the source observable items and then serve the results from cache.
...
Observable<<RealmResults<Account>> cachedResult = getAccounts().cache();
Observable<Accounts> accountsObservable = cachedResult.flatMap(...);
Observable<X> xObservable = cachedResult.flatMap(...);
If you would like to avoid caching results you should use Connectable Observables. Usually it only does matter for Hot Observables. Connectable observable does not begin emitting items until its Connect method is called. You can use publish operator to convert to Connectable Observable.
ConnectableObservable<<RealmResults<Account>> connectebleObservable = getAccounts().publish();
Observable<Accounts> accountsObservable = connectebleObservable .flatMap(...);
Observable<X> xObservable = connectebleObservable .flatMap(...);
//You must subscribe before connect
accountsObservable.subsribe(...);
xObservable.subscribe(...);
//start emiting data
connectebleObservable.connect();
The important catch here is that you must subscribe before connect - to avoid data loss - otherwise you must use replay operator, which is similar to cache operator, but used for connectable observable
And what about share ?
It create ConnectableObservable and exposes it as regular Observable. First subscription automatically causes connection and emission.
Share used in your case, without replay may cause data loss or multiple executions depending on timing.
for example for 2 subscribers and one item int the stream you may have fallowing cases:
2 subscriptions created before onNext - works as expected.
second subscription created after onNext but before onComplete - second subscription gets only onComplete
second subscriptinon created after onComplete - 2 executions wihtout caching