gpt4 book ai didi

android - 为什么 Parse Server saveEventually with callback 花了这么长时间?

转载 作者:行者123 更新时间:2023-12-05 06:16:35 25 4
gpt4 key购买 nike

我正在为我的 Android 应用程序使用 Parse Server,并且一切正常,但每次我在新的或旧的 ParseObject 上调用 saveEventually 时,它都需要真的很久了。有时1个项目返回回调超过1分钟。

有人遇到过这个问题吗?

例子:

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)
}
}

当我将其替换为带回调的 saveInBackground 时也会记录(仍然 30 秒):

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

更新:所以我从解析仪表板中发现,ParseObject 立即保存为表中的记录,但 saveEventually 的回调在 30 秒 - 2 分钟后发送。

更新 2:如果用户在线(带回调),我还尝试使用 saveInBackground()。这也花了 30 秒到 2 分钟 回调返回。 100 毫秒 后对象被保存到解析数据库中的所有数据(从解析仪表板检查)。

然后我以为ParseSDK线程有问题,所以我在Coroutine里面使用了save()。同样的问题发生在这里,save() 花费了 最多 2 分钟 来执行。

协程代码:

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)
}
}
}
}

AndroidParseSDK 中的回调存在一些严重问题,我不知道是什么原因导致的。服务器端没有异常没有错误。

更新 3:经过更深入的调查,我发现哪个功能需要很长时间才能继续。

ParseObject.State result = saveTask.getResult();

大约 30 秒 - 2 分钟 进入下一行代码。这是我在 SDK 中可以获得的最低级别的功能。

内部函数 save()saveInBackground() 在 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();
}
});
}
});
}

最佳答案

来自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.

这可能需要很长时间,因为使用 saveEventually 您基本上是在说“尽快保存”。如果您想“尽快保存”,请按照 docs 中的说明使用 saveInBackground .

进一步说:

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.

这意味着您可以在本地多次保存和修改对象,一旦重新建立网络连接,最新版本将存储在数据库中。

关于android - 为什么 Parse Server saveEventually with callback 花了这么长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62044460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com