/** * getWishList로 전체 찜 목록을 가져왔을때 호출되는 함수. 기존의 Wish들을 삭제하고, 파라메터의 어레이로 다시 채운다. * * @param arr */ public void setAllWishList(JSONArray arr) { // Realm Database ********************************************************************** // Obtain a Realm instance Realm realm = Realm.getInstance(mContext); // remove all realm.beginTransaction(); realm.allObjects(WishObject.class).clear(); for (int i = 0; i < arr.length(); i++) { try { JSONObject jo = arr.getJSONObject(i); JSONObject asset = jo.getJSONObject("asset"); String assetId = asset.getString("assetId"); RealmResults<WishObject> results = mRealm.where(WishObject.class).equalTo("sAssetId", assetId).findAll(); if (results.size() > 0) { // skip } else { // 객체를 생성하거나 데이터를 수정하는 작업을 수행한다. WishObject wish = realm.createObject(WishObject.class); // Create a new object wish.setsAssetId(assetId); wish.setsPhoneNumber(jo.getString("phoneNumber")); wish.setsUserId(jo.getString("userId")); } } catch (JSONException e) { e.printStackTrace(); } } // 트랜잭션 종료 realm.commitTransaction(); // Realm Database ********************************************************************** }
public void removeWWishAsset(String assetId) { Realm realm = Realm.getInstance(mContext); realm.beginTransaction(); RealmResults<WishObject> results = mRealm.where(WishObject.class).equalTo("sAssetId", assetId).findAll(); if (results.size() > 0) { WishObject obj = results.get(0); obj.removeFromRealm(); } realm.commitTransaction(); }