我经常使用writeToRealm方法。我需要从不同的线程中使用它并切断样板代码。这项任务的更好方法是什么?
SELECT mp.PerTPay,bz.BizCode,bz.BizName,count(mp.PerTPay) PerCount, count(mp2.PerTPay) PerCount2 FROM tb_perbiz bz
LEFT JOIN ms_per mp ON BizCode=mp.PerBiz AND mp.PerTPay = 1
LEFT JOIN ms_per mp2 ON BizCode=mp2.PerBiz AND mp2.PerTPay = 2
GROUP BY bz.BizCode,mp.PerTPay
ORDER BY bz.BizCode,mp.PerTPay
}
我打算使用下面的构造(或类似的东西),但我无法创建正确的构造:
private boolean writeToRealm(String user, String id) {
SubscriberObject subscriber = new SubscriberObject();
if(mRealm.where(SubscriberObject.class)
.equalTo(SubscriberObject.ID,id).findAll().isEmpty()
&&mRealm.where(SubscriberObject.class)
.equalTo(SubscriberObject.USERNAME,user).findAll().isEmpty()) {
subscriber.setId(id);
subscriber.setUsername(user);
mRealm.beginTransaction();
mRealm.insert(subscriber);
mRealm.commitTransaction();
return true;
}
return false;
或者我可能需要为此任务创建一个带有looper的线程类?
如何更好地将此方法和类似方法集成到干净的架构中?
答案 0 :(得分:1)
我认为你只是在寻找
private boolean writeToRealm(String user, String id) {
try(Realm realm = Realm.getDefaultInstance()) {
if(realm.where(SubscriberObject.class).equalTo(SubscriberObject.ID,id).count() <= 0L
&& realm.where(SubscriberObject.class).equalTo(SubscriberObject.USERNAME,user).count() <= 0L) {
final SubscriberObject subscriber = new SubscriberObject();
subscriber.setId(id);
subscriber.setUsername(user);
realm.executeTransaction(r -> r.insert(subscriber));
return true;
}
}
return false;
}
答案 1 :(得分:0)
我认为这可能是一种更简单的解决方案:
public static Boolean writeToRealm(String user, String id) {
Realm realm = Realm.getDefaultInstance();
SubscriberObject subscriber = new SubscriberObject();
if (realm.where(SubscriberObject.class).equalTo("ID", id).or().equalTo("USERNAME", user).findAll().isEmpty()){
subscriber.setId(id);
subscriber.setUsername(user);
realm.beginTransaction();
realm.insert(subscriber);
realm.commitTransaction();
realm.close();
return true;
}
realm.close();
return false;
}
如果你需要一些解释,请告诉我,我会实施它:)
PS:如果我误解了你的问题,请告诉我!
答案 2 :(得分:0)
异步事务支持的工作方式与当前的executeTransaction相同,但不是在同一个线程上打开Realm,它将为您提供在不同线程上打开的后台Realm。如果您希望在事务完成或失败时收到通知,也可以注册回调。
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Dog dog = realm.where(Dog.class).equalTo("age", 1).findFirst();
dog.setName("Fido");
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
Log.d("REALM", "All done updating.");
Log.d("BG", t.getName());
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
// transaction is automatically rolled-back, do any cleanup here
}
});
答案 3 :(得分:0)
首先,如果要异步执行事务,则不能拥有import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellType.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.FileInputStream;
import java.util.List;
import java.util.ArrayList;
class ReadExcelRichTextCells {
public static void main(String[] args) throws Exception {
Workbook wb = WorkbookFactory.create(new FileInputStream("ExcelRichTextCells.xlsx"));
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellTypeEnum()) {
case STRING:
XSSFRichTextString richtextstring = (XSSFRichTextString)cell.getRichStringCellValue();
String textstring = richtextstring.getString();
List<String> textparts = new ArrayList<String>();
if (richtextstring.hasFormatting()) {
for (int i = 0; i < richtextstring.numFormattingRuns(); i++) {
if (richtextstring.getFontOfFormattingRun(i)==null || !richtextstring.getFontOfFormattingRun(i).getStrikeout()) {
int indexofformattingrun = richtextstring.getIndexOfFormattingRun(i);
String textpart = textstring.substring(indexofformattingrun,
indexofformattingrun + richtextstring.getLengthOfFormattingRun(i));
String[] textpart_split = textpart.split("-");
for (int j = 0; j < textpart_split.length; j++){
String text = textpart_split[j].replace("\n", "").trim();
if (!"".equals(text)) textparts.add(text);
}
}
}
} else {
textparts.add(textstring);
}
System.out.println(textparts);
break;
//...
default:
System.out.println("default cell"); //should never occur
}
}
}
wb.close();
}
}
返回类型。您必须将boolean
传递结果返回给调用者,或者您必须选择其他方法,例如RxJava。
只是举个例子。
RxJava方式(因为这是最简单的方法):
Interface
您在所需的线程/计划上订阅了返回的public static Flowable<Boolean> writeToRealm(final String user,final String id) {
return Flowable.fromCallable(
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Realm realm = Realm.getDefaultInstance();
if(realm.where(SubscriberObject.class)
.equalTo(SubscriberObject.ID,id).findAll().isEmpty()
&&realm.where(SubscriberObject.class)
.equalTo(SubscriberObject.USERNAME,user).findAll().isEmpty()) {
SubscriberObject subscriber = new SubscriberObject();
subscriber.setId(id);
subscriber.setUsername(user);
realm.beginTransaction();
realm.insert(subscriber);
realm.commitTransaction();
mRealm.close();
return true;
}
mRealm.close();
return false;
}
});
}
,以便在该特定线程上执行事务。