你可能记得我之前关于Green DAO双向关系的问题,我的聊天内容包括对话和消息.
每条消息都有一个父对话,对话中有一条消息列表.
当我尝试添加新消息时,只要我不关闭应用程序,它就能完美运行.
这是我用于向现有对话添加消息的代码:
我现在有我的代码执行此操作:
Conversation conv = getConversation();
List<Message> list = conv.getMessageList();
Message msg = new Message();
msg.setConversationId(conv,getId());
MessageDao.insert(msg);
list.add(msg);
conv.update();
// ConversationDao.update(conv);
Run Code Online (Sandbox Code Playgroud)
当我下次打开应用程序并调用时conv.getMessageList(),结果为空ArrayList.
我究竟做错了什么?
编辑:
我改变了我的代码,现在它看起来像这样:
Conversation conv= getCOnversation();
Message msg = new Message();
msg.setConversation(conv);
MessageDao.insert(msg);
conv.resetMessageList();
Run Code Online (Sandbox Code Playgroud)
不幸的是,现在比以往任何时候都更多(即使在同一次运行中),仍然调用conv.getMessageList()返回空java.list.
编辑2:
这里是生成器的代码(这不是真正的代码,而只是对问题很重要的代码.
Entity message = schema.addEntity("Message");
message.addIdProperty().autoincrement();
Entity conversation =schema.addEntity("Conversation");
conversation.addIdProperty().autoincrement();
Property parentConversation = message.addLongProperty("parentConversation")
.getProperty();
Property messages = conversation.addLongProperty("messages").getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, messages);
Run Code Online (Sandbox Code Playgroud)
编辑3
现在,而不是调用conv.getMessageList(); …
如果我理解正确,在使用DB时,我必须执行以下操作
DaoMaster.OpenHelper helper = new DaoMaster.OpenHelper(this, "test-db", null) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试在不扩展活动或服务的类中执行此操作,我只是无法通过其上下文.
什么是正确的方法来打开我的数据库?它应该在哪里完成?
如果你能提供除官方greendao之外的一些教程链接(我在那里找不到答案),那就太好了.
我使用GreenDAO.但是,抛出此异常:
"de.greenrobot.dao.DaoException: Entity is detached from DAO context".
Run Code Online (Sandbox Code Playgroud)
在生成的代码中,我找到了这段代码.
/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getPeerGroupDao() : null;
}
Run Code Online (Sandbox Code Playgroud)
谁知道GreenDAO什么时候打电话呢?另外,如何触发它来调用.
谢谢.
目前我正在一个类中创建greenDAO数据库连接(它在每个静态方法中打开连接)并在我需要的地方使用它.但我不确定这是否是最佳方式.谁能建议一个更好的方法呢?
我的代码:
import com.knowlarity.sr.db.dao.DaoMaster;
import com.knowlarity.sr.db.dao.DaoMaster.DevOpenHelper;
import com.knowlarity.sr.db.dao.DaoSession;
import com.knowlarity.sr.db.dao.IEntity;
public class DbUtils {
private static Object lockCallRecord =new Object();
private DbUtils(){};
public static boolean saveEntity(Context context , IEntity entity){
boolean t=false;
DevOpenHelper helper=null;
SQLiteDatabase db=null;
DaoMaster daoMaster=null;
DaoSession daoSession =null;
try{
helper = new DaoMaster.DevOpenHelper(context, IConstant.DB_STRING, null);
db = helper.getReadableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
//Some business logic here for fetching and inserting the data.
}catch (Exception e){
Log.e("saveEntity", e.getStackTrace().toString());
}finally{
if(daoSession!=null)daoSession.clear();
daoMaster=null;
if(db.isOpen())db.close();
helper.close();
}
return …Run Code Online (Sandbox Code Playgroud) 我正在使用GreenDao来存储来自REST服务的大量数据.
我的很多实体都与关系联系在一起.一切都很好,但明天我必须实现rockolid工作流程.
当我加载数据时,我必须检查是否发生错误.如果是这样,我必须确保SQLite DB中没有存储任何内容.
通常我会在发生异常的情况下使用事务和回滚,否则将提交到db.
现在我只使用insertordelete来保存实体,每次我创建一个对象.
实现这个的方法是什么?
我正在使用greenDAO并且我已经成功生成了所有必需的类和实体,我可以看到我的表已经创建了,但是在更换行的断点之后,我收到一个错误告诉我"没有这样的表存在错误".
try {
appTimeUsageDao.insertOrReplace(appStats);
//} catch (DaoException e) {
} catch (Exception e) {
Log.e("Error", "Some exception occurred", e);
Log.e("APP_TAG", Log.getStackTraceString(e));
}
Run Code Online (Sandbox Code Playgroud) 有谁知道如何select * from table在greenDAO中进行简单操作并将其放入实体中?我对此做了一些研究,我无法得到任何简单的例子.这是我到目前为止:
public void storeAppTimeUsageData(AppTimeUsage stats) {
List<AppTimeUsage> items = new ArrayList<>();
//appTimeUsageDao = DeviceInsightApp.getSession(this, true).getAppTimeUsageDao();
try {
// master
appTimeUsageDao.insertOrReplace(stats);
//} catch (IOException e) {
} catch (Exception e) {
Log.e("Error", "Some exception occurred", e);
Log.e("APP_TAG", "STACKTRACE");
Log.e("APP_TAG", Log.getStackTraceString(e));
}
String sql = "SELECT * FROM APP_TIME_USAGE ";
Cursor c = appTimeUsageDao.getDatabase().rawQuery(sql, null);
int offset = 0;
int d ;
int cd ;
String e = "";
while (c.moveToNext()) {
AppTimeUsage atu AppTimeUsage(
c.getLong(0);
//long …Run Code Online (Sandbox Code Playgroud) 我相信这应该很简单,但我似乎无法找到解决方案.
假设我有一个包含5个字段的表(id,颜色,大小,扫描,预期)
如何获得预期大于扫描的所有行?
我试过了
mydao.Property.Expected.gt(mydao.Property.Scanned).list()
Run Code Online (Sandbox Code Playgroud)
但是这会返回0行.使用LOG_SQL布尔我可以看到它正在查找属性字段,而不是字段的值.
D/greenDAO: Values for query: [0, de.greenrobot.dao.Property@33ecce97]
Run Code Online (Sandbox Code Playgroud) 我有两个Observables。observableDisconnectContact负责调用 API 请求删除联系人并正常工作。observableDeleteContact如果联系人从数据库中删除,则第二个发出 true 或 false。我用greeDao.
Observable<Boolean> observableDisconnectContact = apiClient.observableDisconnectContact(contactModel.getId())
Observable<Boolean> observableDeleteContact = contactModelRxDao.deleteByKey(contactModel.getDbId())
Run Code Online (Sandbox Code Playgroud)
我想将两者结合起来,但第二个 observable 应该在第一个完成时开始并返回 true。我考虑使用concat()和first()。但我必须知道两个流都发出结果是真的。所以我使用combineLatest()or zip()。但这不是一个好主意,因为两个流都在同一时间运行。我注意到first()运算符不适用于zip()and combineLatest()。
Observables如果第一次返回 false 并且两个流的结果应该是一个结果,我如何将第二个开始的位置与第一个流之后的位置结合起来。
Observable.combineLatest(observableDisconnectContact, observableDeleteContact, new Func2<Boolean, Boolean, Boolean>() {
@Override
public Boolean call(Boolean isDisconnectSuccess, Boolean isRemoveSuccess) {
return isDisconnectSuccess && isRemoveFromDatabaseSuccess;
}
}).subscribe(new Subscriber<Boolean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
} …Run Code Online (Sandbox Code Playgroud) 我正在使用greenDAO for Android,我想编辑greenDAO生成器,因为它会覆盖我每次在数据库上更改内容时需要的@SerializedName.
所以我通过文件 - >新建 - >导入模块将https://github.com/greenrobot/greenDAO中的 greenDAO添加到我的Android Studio项目中,然后选择了GreenDaoGenerator模块.
接下来,我构建我的java模块来生成greenDAO数据库,它使用GreenDaoGenerator模块作为依赖项,我得到一个例外,找不到模板dao.ftl.
有人可以帮帮我吗?我是Android的新手,不检查,为什么它找不到该文件.
Exception in thread "main" java.io.FileNotFoundException: Template "dao.ftl" not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:742)
at freemarker.template.Configuration.getTemplate(Configuration.java:665)
at de.greenrobot.daogenerator.DaoGenerator.<init>(DaoGenerator.java:65)
at com.example.GreenDaoCreater.main(GreenDaoCreater.java:39)
Run Code Online (Sandbox Code Playgroud)
编辑:这是项目结构的截图.我希望它有所帮助.

greendao ×10
android ×8
sqlite ×3
orm ×2
dao ×1
java ×1
rollback ×1
rx-android ×1
rx-java ×1
transactions ×1