0
我有一個導致崩潰的領域線程,我不知道爲什麼這是不被允許的,或者如何繞過它。這裏是發生了什麼事情的一些示例代碼:IntentService領域 - 線程創建崩潰
public class UploadPostService extends IntentService {
public UploadPostService() {
super("UploadPostService");
}
@Override
protected void onHandleIntent(Intent intent) {
String uniqueCode = intent.getStringExtra("uniqueCode");
OurApi api = OurApi.build(this, Application.apiRoot);
final Realm r = Realm.getDefaultInstance();
final RealmResults<Post> thePosts = r.where(Post.class)
.equalTo("post.code", uniqueCode)
.findAll();
if (thePosts != null && thePosts.size() > 0) {
for (final Post post : thePosts) {
api.uploadMedia(paramsToUpload, new Callback<Post>() {
@Override
public void success(Post postResponse, Response response) {
if (post.isValid()) {
r.beginTransaction();
post.setAField(blah); // CRASHES HERE
r.commitTransaction();
}
}
}
etc...
的API與改造通話結束後,它崩潰的任何字段的設置與異常的「郵報」對象:
"java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created."
我很好奇最乾淨的解決方案應該是什麼?假設回調是在一個不同的線程比IntentService一個..我需要更新實際的郵政,它不讓我;我嘗試創建單獨的Realm實例,但它不會讓我更新Post,因爲它不是(顯然)從同一實例查詢的。
這對我們的代碼至關重要,所以我有點難住。預先感謝您的任何建議!