java - Android, how to initiate realm in AsyncTask? -
i have asynctask in application below. have fetch data realm(which stored in activity) asynctask. below asynctask code:
public class makeasyncrequest extends asynctask<object, string, makeasyncrequest.responsedatatype>{ public context asynccontext; private makeasyncrequest(context context){ asynccontext = context; } private static final okhttpclient client = new okhttpclient(); private static final mediatype json = mediatype.parse("application/json; charset=utf-8"); private makerequest.responsehandler handler; private string method; private user user; private realm realm; class responsedatatype { inputstream inputstream; string string; string cookie; } makeasyncrequest(makerequest.responsehandler responsehandler, string type) { method = type; handler = responsehandler; } @override protected responsedatatype doinbackground(object... params) { try { requests requests = new requests((context) params[0]); string url = params[1].tostring(); string bodyjson = null; if(method.equals("put") || method.equals("post")) { bodyjson = params[2].tostring(); } final request.builder builder; response response; requestbody body; switch (method) { case "get": builder = new request.builder().url(url); break; case "download": builder = new request.builder().url(url); break; case "post": body = requestbody.create(json, bodyjson); builder = new request.builder() .url(url) .post(body) .addheader("cookie", "key=value"); break; case "put": body = requestbody.create(json, bodyjson); builder = new request.builder() .url(url) .put(body); break; default: builder = new request.builder().url(url); } builder.addheader("accept", "application/json"); realm = realmcontroller.initialize(this).getrealm(); final realmresults<user> users = realm.where(user.class).findallasync(); user = users.first(); if(user.getcookie() !== null && !user.getcookie().isempty()){ builder.addheader("cookie", "user.getcookie()"); } response = client.newcall(builder.build()).execute(); responsedatatype responsedatatype = new responsedatatype(); if(method.equals("download")) { responsedatatype.inputstream = response.body().bytestream(); } else { responsedatatype.string = response.body().string(); responsedatatype.cookie = response.headers().get("set-cookie"); cookiemanager cmanager = new cookiemanager(null, cookiepolicy.accept_all); cmanager.getcookiestore().getcookies(); } return responsedatatype; } catch (ioexception e) { return null; } } @override protected void onpostexecute(responsedatatype response) { try { if (method.equals("download")) { handler.onfinishcallback(response.inputstream); }else { handler.onfinishcallback(response.string, response.cookie); cookiemanager cmanager = new cookiemanager(null, cookiepolicy.accept_all); cmanager.getcookiestore().getcookies(); } }catch (exception e){ log.d("hexceptionerror", e.tostring()); handler.onfinishcallback("{\n" + " \"error\": \"error\"\n" + "}",""); } }
i received access error - "realm access incorrect thread. realm objects can accessed on thread created." whenever control tries execute realm results or first object realm.
below realmcontroller created control realm instance:
public class realmcontroller {`public static realmcontroller initialize(activity activity) { if (instance == null) { instance = new realmcontroller(activity.getapplication()); } return instance; } public static realmcontroller initialize(makeasyncrequest activity) { if (instance == null) { instance = new realmcontroller(activity.); } return instance; } }`
i have user model(realm object) setters , getters.
the point - cannot create , access realm on different threads, i.e. create realm instance in activity , use in .doinbackground() method. create , release realm before , after transaction. there may issue - don't register quer observer on background thread in asynctask - doesn't have looper initialized - use main thread or handlerthread. release realm after no longer needed (you didn't in code), because realm has limited number of instances.
Comments
Post a Comment