本文整理汇总了Java中io.realm.Realm.executeTransactionAsync方法的典型用法代码示例。如果您正苦于以下问题:Java Realm.executeTransactionAsync方法的具体用法?Java Realm.executeTransactionAsync怎么用?Java Realm.executeTransactionAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.realm.Realm
的用法示例。
在下文中一共展示了Realm.executeTransactionAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processAndAddData
import io.realm.Realm; //导入方法依赖的package包/类
private void processAndAddData(final Realm realm, final String sectionKey, final List<NYTimesStory> stories) {
if (stories.isEmpty()) return;
realm.executeTransactionAsync(r -> {
for (NYTimesStory story : stories) {
Date parsedPublishedDate = inputDateFormat.parse(story.getPublishedDate(), new ParsePosition(0));
story.setSortTimeStamp(parsedPublishedDate.getTime());
story.setPublishedDate(outputDateFormat.format(parsedPublishedDate));
// Find existing story in Realm (if any)
// If it exists, we need to merge the local state with the remote, because the local state
// contains more info than is available on the server.
NYTimesStory persistedStory = r.where(NYTimesStory.class).equalTo(NYTimesStory.URL, story.getUrl()).findFirst();
if (persistedStory != null) {
// Only local state is the `read` boolean.
story.setRead(persistedStory.isRead());
}
// Only create or update the local story if needed
if (persistedStory == null || !persistedStory.getUpdatedDate().equals(story.getUpdatedDate())) {
story.setApiSection(sectionKey);
r.copyToRealmOrUpdate(story);
}
}
}, throwable -> Timber.e(throwable, "Could not save data"));
}
示例2: insertOrUpdateElephant
import io.realm.Realm; //导入方法依赖的package包/类
static public void insertOrUpdateElephant(final Elephant elephant, final List<Document> documents) {
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
if (elephant.id == -1) {
elephant.id = getNextId(bgRealm, Elephant.class, Elephant.ID);
}
bgRealm.insertOrUpdate(elephant);
for (Document document : documents) {
if (document.id == -1) {
document.id = getNextId(bgRealm, Document.class, Document.ID);
}
document.elephant_id = elephant.id;
bgRealm.insertOrUpdate(document);
}
}
});
}
示例3: save
import io.realm.Realm; //导入方法依赖的package包/类
public void save(Date date,long num)
{
Realm realm = Realm.getDefaultInstance();
realmAsyncTask = realm.executeTransactionAsync(
new StepTransaction(date, num),
new SuccessTransaction(realmAsyncTask),
new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
error.printStackTrace();
Log.d("realm", "insert error");
}
}
);
realm.close();
}
示例4: saveOrUpdate
import io.realm.Realm; //导入方法依赖的package包/类
public static <O extends RealmObject> void saveOrUpdate(Realm realmInstance,O object, OnTransactionCallback callback) {
Realm realm = realmInstance;
realm.executeTransactionAsync(
realm1 -> realm1.copyToRealmOrUpdate(object),
callback::onSuccess,
callback::onError
);
}