背景:
我正在开发一个使用 parse.com 作为我的后端的 Android 群消息应用程序。
在职的:
我有一个 ParseQueryAdapter,它使用以下方法从本地数据存储中查询 ChatGroups:
public ChatGroupAdapter(final Context context) { super(context, new ParseQueryAdapter.QueryFactory<ChatGroup>() { public ParseQuery<ChatGroup> create() { ParseQuery<ChatGroup> chatGroupQuery = ParseQuery.getQuery(ChatGroup.class); chatGroupQuery.fromLocalDatastore(); Log.d(ChatGroup.class.getName(), "Constructor query: " + chatGroupQuery + " made"); //noinspection unchecked return chatGroupQuery; } }); }
包含它的片段通过添加一个
ParseQueryAdapter.OnQueryLoadListener
. 如果ParseQueryAdapter
无法ChatGroup
从本地数据存储中检索 s,它会从 parse 中检索它,将其固定,然后触发ParseQueryAdapter
从本地数据存储重试加载:.loadObjects();
。这是我的片段中的代码片段:@Override public void onLoaded(List<ChatGroup> groups, Exception e) { l.d("onLoaded called"); if (e != null) { e.printStackTrace(); updateLocalDatastore(); } else if (groups == null) { l.w("Received null group list."); updateLocalDatastore(); } else if (groups.size() == 0) { l.w("Received group list of size zero."); updateLocalDatastore(); } else { l.d("Chat groups loaded successfully"); } } private void updateLocalDatastore() { final ParseQuery<ChatGroup> groupQuery = ParseQuery.getQuery(ChatGroup.class); groupQuery.findInBackground(new FindCallback<ChatGroup>() { @Override public void done(final List<ChatGroup> chatGroupList, ParseException e) { if (e != null) { e.printStackTrace(); } else { if (chatGroupList != null) { ParseObject.pinAllInBackground(chatGroupList, new SaveCallback() { @Override public void done(ParseException e) { if (e != null) { e.printStackTrace(); } else { l.d("Successfully pinned: " + chatGroupList.size() + " groups to local datastore. Reloading adapter"); groupAdapter.loadObjects(); } } }); } else { l.e("Received null group list from cloud! " + "Local datastore update failed!"); } } } }); }
结果:
这是一个简化的执行日志:
ChatFragment: onCreate
ChatFragment﹕ Initializing chat groups
ChatFragment﹕ Loading chat groups
ChatFragment: onStart
ChatFragment: onResume
ChatFragment﹕ onLoaded called
ChatFragment﹕ Received group list of size zero.
ChatFragment﹕ Successfully pinned: 3 groups to local datastore. Reloading adapter
ChatFragment﹕ Loading chat groups
ChatFragment﹕ onLoaded called
ChatFragment﹕ Received group list of size zero.
ChatFragment﹕ Successfully pinned: 3 groups to local datastore. Reloading adapter
ChatFragment﹕ Loading chat groups
ChatFragment﹕ onLoaded called
ChatFragment﹕ Received group list of size zero.
ChatFragment﹕ Successfully pinned: 3 groups to local datastore. Reloading adapter
ChatFragment﹕ Loading chat groups
ChatFragment﹕ onLoaded called
ChatFragment﹕ Received group list of size zero.
对我来说很明显ParseQueryAdapter
应该第一次检索 0 的列表,这会触发从解析updateLocalDatastore()
中提取 3ChatGroup
秒,固定它们,然后它应该从本地数据存储中接收 3。但是,从日志中可以看出,即使成功固定它们,它也会继续从本地数据存储中找到 0 !ChatGroup
项目设置似乎很好:
Parse.enableLocalDatastore(this);
Parse.initialize(this, AppProps.properties.appId, AppProps.properties.clientKey);
ParseUser.enableRevocableSessionInBackground();
Parse.setLogLevel(Parse.LOG_LEVEL_DEBUG);
ParseObject.registerSubclass(ChatGroup.class);
我究竟做错了什么?!我错过了什么吗?
我实现了完全相同的逻辑ParseUser
并且它完美地工作!做同样的事情似乎有问题GroupChat
:
@ParseClassName("ChatGroup")
public class ChatGroup extends ParseObject {
有人说这是本地数据存储中关系的解析错误。但我什至没有使用关系。即使是来自本地数据存储的简单查询也不起作用。