我有一個應用程序,我想要使用ISBN搜索圖書並將其顯示在屏幕上。 Book
對象存儲在以ISBN作爲主鍵的Realm數據庫中。如果它們不存儲在數據庫中,則它們將從服務器異步檢索(ASyncTask),然後存儲在數據庫中。Realm數據庫 - 如何構建我的Android應用程序
現在,我不熟悉Realm以及如何使用它。
在下面的代碼中,ViewBookActivity
和MainController
是否在同一個線程上運行,因此使用相同的數據庫實例?從「靜態類」返回Realm對象會成爲問題嗎?
如何保證致電MainController.getBook()
總是返回一本書?現在它的工作方式是,當調用getBook並且該書不在數據庫中時,它將返回null。
是每個使用MainController唯一方法的Activity的changelisteners嗎?如果可能的話,我希望避免在活動中使用/引用Realm,並讓它們通過MainController獲取對象。
public class RealmActivity extends Activity {
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
Realm.deleteRealm(realmConfiguration); // Clean slate
Realm.setDefaultConfiguration(realmConfiguration); // Make this Realm the default
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
public class ViewBookActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
ISBN = intent.getStringExtra("ISBN");
setContentView(R.layout.activity_scan_result);
setBook(ISBN);
}
public void setBook(String ISBN) {
Book b = MainController.getBook(ISBN);
// Display book on screen
}
}
public class MainController {
static Realm realm = Realm.getDefaultInstance();
public static Book getBook(String isbn) {
Book book = realm.where(Book.class)
.equalTo("isbn", isbn)
.findFirst();
if (book == null) {
NetworkController.getBook(isbn);
} else {
return book;
}
return null;
}
}
public class NetworkController {
private static BookHandler bookHandler = new BookHandler();
public static void getBook(String isbn) {
NetworkHelper.sendRequest(HTTPRequestMethod.GET,
"/books/" + isbn, bookHandler);
}
}
public class NetworkHelper {
private static String host = "http://crowdshelf-dev.herokuapp.com";
public static void sendRequest(final HTTPRequestMethod requestMethod, final String route, final ResponseHandler responseHandler) {
new AsyncTask<Void, Void, Void>() {
@Override
protected void doInBackground(Void... params) {
try {
URL url = new URL(host + "/api" + route);
Log.d("NETDBTEST", "NetworkHelper request: " + requestMethod.toString() + " URL: " + url.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
BufferedReader bReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
builder.append(line).append("\n");
}
String jsonString = builder.toString();
responseHandler.handleJsonResponse(jsonString);
return null;
}
}
}.execute();
}
}
public class BookHandler implements ResponseHandler {
@Override
public void handleJsonResponse(String jsonString) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.createOrUpdateObjectFromJson(Book.class, jsonString);
realm.commitTransaction();
realm.close();
}
}
public class Book extends RealmObject{
@PrimaryKey
private String isbn;
// Other fields, getters and setters
}
iii)iv)我可以在'BookHandler'中用'ViewBookActivity'中的監聽器使用Event Bus或Otto來更新UI嗎?我想保留我的'BookHandler',因爲我有許多處理程序,它們的工作方式不同,我不希望所有代碼都在'onPostExecution'中混合在一起。 – tsorn