我正在执行服务的代码。该代码的工作方式如下:
// check the LocalDataStore
// if the localDataStore is empty then fetch new jokes and update the localDataStore
// then try again from the first step.
// otherwise, continue with further logic
private void fetchAndUploadJokesFromLocalDataStore() {
Log.v("uploading", "taking joke from the LocalDataStore");
ParseQuery<ParseObject> query = ParseQuery.getQuery("JokesDataToBeUploaded");
query.orderByDescending("objectId");
query.fromLocalDatastore();
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
if (list.size() > 0) {
Log.v("uploading", "found " + list.size() + " jokes in LocalDataStore");
} else {
Log.v("uploading", "localDataStore is empty, fetching new jokes");
SharedPreferences configData = getSharedPreferences("configData", 0);
uploadJokes(configData.getInt("uploadHowManyJokes", 1));
Log.v("Service", "uploadJokes called. uploadHowManyJokes=" + configData.getInt("uploadHowManyJokes", 1));
}
} else {
}
} ... // more methods follow but the brackets are closed correctly in real code.
}这里调用uploadJokes()方法。
public void uploadJokes(int numberOfJokes) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("JokesDataCollection");
query.whereEqualTo("published", false);
query.orderByDescending("objectId");
query.setLimit(numberOfJokes); // limit to at most "n" results
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> feedObjectList,
ParseException e) {
if (e == null) {
Log.d("Uploading Joke", "Jokes Retrieved: " + feedObjectList.size());
if (feedObjectList.size() > 0) {
updateLocalFeedWithNewData(feedObjectList);
}
} else {
Log.d("Uploading Joke", "Error: " + e.getMessage());
showNotification("", "");
stopSelf();
}
}
});
}在这里,可以找到项目,并调用uploadLocalFeedWithNewData()
private void updateLocalFeedWithNewData(final List<ParseObject> newFeed) {
// Release any objects previously pinned for this query.
final ParseObject feed = new ParseObject("JokesDataToBeUploaded");
ParseObject.unpinAllInBackground("JokesDataToBeUploaded", new DeleteCallback() {
public void done(ParseException e) {
if (e == null) {
// Cache the new results.
Log.v("pinning", "unpinned all");
ParseObject.pinAllInBackground("JokesDataToBeUploaded", newFeed, new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Log.v("pinning", "pinned successfully");
fetchAndUploadJokesFromLocalDataStore();
} else {
Log.v("pinning", "pinning FAILED");
// todo try again, either by restarting the service and getting entirely new jokes, or by trying again on the same object list
stopSelf();
}
}
});
} else {
Log.v("pinning", "UNpinning FAILED");
Log.v("ParseException caught", e.getMessage());
stopSelf();
}
}
}
);
}最后,fetchAndUploadJokesFromLocalDataStore()方法(这是本文中的第一个方法)在成功保存后再次被调用。现在,该方法应该从LocalDataStore中检索项,但它再次检索零个对象。
这是日志:
10-30 22:00:09.429 4102-4102/com.appsbyusman.jokesmanager V/Service: Started
10-30 22:00:09.459 4102-4102/com.appsbyusman.jokesmanager V/uploading: taking joke from the LocalDataStore
10-30 22:00:09.519 4102-4102/com.appsbyusman.jokesmanager V/uploading: localDataStore is empty, fetching new jokes
10-30 22:00:09.529 4102-4102/com.appsbyusman.jokesmanager V/Service: uploadJokes called. uploadHowManyJokes=1
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):Reading from variable values from setDefaultValuesToVariables
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):isSBSettingEnabled false
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):isShipBuild true
10-30 22:00:09.539 4102-6493/com.appsbyusman.jokesmanager I/System.out: Thread-12964(ApacheHTTPLog):SmartBonding Enabling is false, SHIP_BUILD is true, log to file is false, DBG is false
10-30 22:00:10.751 4102-6493/com.appsbyusman.jokesmanager I/System.out: ParseRequest.NETWORK_EXECUTOR-thread-1 calls detatch()
10-30 22:00:10.761 4102-4102/com.appsbyusman.jokesmanager D/Uploading Joke: Jokes Retrieved: 1
10-30 22:00:10.791 4102-4102/com.appsbyusman.jokesmanager V/pinning: unpinned all
10-30 22:00:10.831 4102-4102/com.appsbyusman.jokesmanager V/pinning: pinned successfully
10-30 22:00:20.841 4102-6525/com.appsbyusman.jokesmanager V/uploading: taking joke from the LocalDataStore
10-30 22:00:20.941 4102-4102/com.appsbyusman.jokesmanager V/uploading: localDataStore is empty, fetching new jokes
10-30 22:00:20.941 4102-4102/com.appsbyusman.jokesmanager V/Service: uploadJokes called. uploadHowManyJokes=1
10-30 22:00:21.321 4102-6809/com.appsbyusman.jokesmanager I/System.out: ParseRequest.NETWORK_EXECUTOR-thread-2 calls detatch()
10-30 22:00:21.351 4102-4102/com.appsbyusman.jokesmanager D/Uploading Joke: Jokes Retrieved: 1
10-30 22:00:21.521 4102-4102/com.appsbyusman.jokesmanager V/pinning: unpinned all
10-30 22:00:21.561 4102-4102/com.appsbyusman.jokesmanager V/pinning: pinned successfully
10-30 22:00:31.571 4102-6837/com.appsbyusman.jokesmanager V/uploading: taking joke from the LocalDataStore
10-30 22:00:31.681 4102-4102/com.appsbyusman.jokesmanager V/uploading: localDataStore is empty, fetching new jokes
10-30 22:00:31.681 4102-4102/com.appsbyusman.jokesmanager V/Service: uploadJokes called. uploadHowManyJokes=1
10-30 22:00:31.971 4102-6981/com.appsbyusman.jokesmanager I/System.out: ParseRequest.NETWORK_EXECUTOR-thread-3 calls detatch()
10-30 22:00:31.991 4102-4102/com.appsbyusman.jokesmanager D/Uploading Joke: Jokes Retrieved: 1
10-30 22:00:32.051 4102-4102/com.appsbyusman.jokesmanager V/pinning: unpinned all
10-30 22:00:32.132 4102-4102/com.appsbyusman.jokesmanager V/pinning: pinned successfully这是一个无限循环,其中ParseRequest.NETWORK_EXECUTOR-线程-3调用一次又一次地被调用
发布于 2015-10-31 16:24:24
我找到答案了!
问题是,我保存了一个名为JokesDataCollection的类,名为JokesDataToBeUploaded。新名称只是一个标识符,用于标识LocalDataStore中存在的项(用于删除、更新等)。然后,我用来查询本地DataStore的类名是"JokesDataToBeUploaded",而从internet获取并保存的实际类名为JokesDataCollection。
因此,我甚至需要调用下面的命令从LocalDatastore中获取条目。
ParseQuery.getQuery("JokesDataCollection");
query.fromLocalDatastore();https://stackoverflow.com/questions/33441645
复制相似问题