gpt4 book ai didi

android - 后台线程中的 Realm 不良行为

转载 作者:行者123 更新时间:2023-11-30 01:18:11 27 4
gpt4 key购买 nike

只有在重新启动应用程序后,才会显示来自后台线程的数据库更改。为什么?如何修复?

public class UILApplication extends Application {

@Override
public void onCreate() {
super.onCreate();

RealmConfiguration config = new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(config);
}

后台是下一段插入数据库的代码:

public void setFollows(List<JSONObject> follows) {
Realm realm = Realm.getDefaultInstance();
List<Follow> finalFollows = new RealmList<>();

try {
for(JSONObject follow:follows){
finalFollows.add(new Follow(follow.getLong("id"),follow.getString("username"), follow.getString("profile_picture"), follow.getString("full_name")));
}
List<Follow> goneFollows = getGoneFollows(finalFollows);
List<Follow> newFollows = getNewFollows(finalFollows);

realm.beginTransaction();

if(goneFollows != null && !goneFollows.isEmpty()){
goneFollows = realm.copyToRealmOrUpdate(goneFollows);
L.d("getHistoryForUpdate goneFollows");
History history = getHistoryForUpdate();
history.getRemoveFollows().clear();
history.getRemoveFollows().addAll(goneFollows);
}

if(newFollows != null && !newFollows.isEmpty()){
newFollows = realm.copyToRealmOrUpdate(newFollows);
L.d("getHistoryForUpdate newFollows");
History history = getHistoryForUpdate();
history.getNewFollows().clear();
history.getNewFollows().addAll(newFollows);
}

finalFollows = realm.copyToRealmOrUpdate(finalFollows);
getFollows().clear();
getFollows().addAll(finalFollows);
realm.commitTransaction();
realm.close();
} catch (JSONException e) {
e.printStackTrace();
}
}

public History getHistoryForUpdate(){
Realm realm = Realm.getDefaultInstance();
String today = DateHandler.getOnlyDate();

History history = realm.where(History.class).equalTo("createDate", today).findFirst();

if(history == null){
L.d("getHistoryForUpdate new");
realm.beginTransaction();
history = new History();
history = realm.copyToRealm(history);
history.setCreateDate(today);
getHistoryList().add(history);
realm.commitTransaction();
}
L.d("getHistoryForUpdate");
realm.close();
return history;
}

在 Fragment 中尝试输入新数据,但只有在您重新启动应用程序后我才能得到

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
realm = Realm.getDefaultInstance();
favorite = realm.where(Favorite.class).equalTo("id", Long.parseLong(userId))
.findFirst();
RealmList<History> historyList = favorite.getHistoryList();
...
}

里面的后台线程都写的很好,可以立即访问数据,但是要从应用程序中获取数据,必须重新启动

最佳答案

<receiver
android:name=".core.receivers.Alarm"
android:process=":remote" />

删除第二个属性后成功

关于android - 后台线程中的 Realm 不良行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37526689/

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