Dans mon application, je dois récupérer des données json depuis l'API de repos. J'utilise Realm comme base de données locale dans l'application. Maintenant, après avoir créé des classes de modèles, j'ai une classe de contrôleurs où j'ai effectué la désérialisation et la fonctionnalité stockée dans Realm db. Mais malheureusement, l'application plante à chaque fois que je clique sur l'option pour voir la page de nouvelles. J'ai expliqué dans le code.L'obtention d'une erreur 'value' n'est pas un objet géré valide lors de l'implémentation de Realm dans l'application Android
NewsRealmModel Classe
public class NewsRealmModel extends RealmObject {
public static final String ID = "id";
private String _id;
private String body;
private String title;
private String updatedDate;
private RealmList<AppImage> appImages;
private String updatedAt;
private TeaserImageSmall teaserImageSmall;
public NewsRealmModel() {
}
public static String getID() {
return ID;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getBody() {
return body;
}
public void setUpdatedDate(String updatedDate) {
this.updatedDate = updatedDate;
}
public RealmList<AppImage> getAppImages() {
return appImages;
}
public void setAppImages(RealmList<AppImage> appImages) {
this.appImages = appImages;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public TeaserImageSmall getTeaserImageSmall() {
return teaserImageSmall;
}
//getting error for this.teaserImageSmall = teaserImageSmall;
public void setTeaserImageSmall(TeaserImageSmall teaserImageSmall) {
this.teaserImageSmall = teaserImageSmall;
}
}
TeaserImageSmall Classe
public class TeaserImageSmall extends RealmObject{
private String alt;
private String src;
public String getSrc() {
return src;
}
public void setSrc(String src) {
this.src = src;
}
}
Maintenant, la classe AppImage
public class AppImage extends RealmObject{
private String _id;
private String alt;
private String src;
//getter and setter
}
La classe contrôleur est
public class NewsController {
private static final String TAG = NewsController.class.getSimpleName();
private UserCallbackListener mListener;
private NewsRestApiManager mApiManager;
private AppImage appImages;
Realm myNews_realm;
public NewsController(NewsPage listener) {
mListener = listener;
mApiManager = new NewsRestApiManager();
}
public void startFetching(){
myNews_realm = Realm.getDefaultInstance();
mApiManager.getNewsApi().getNews(new Callback<String>() {
@Override
public void success(String s, Response response) {
Log.d(TAG, "JSON :: " + s);
try {
JSONArray array = new JSONArray(s);
for(int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
Log.d("-----Start Fetching---", jsonObject.optString("title"));
myNews_realm.beginTransaction();
NewsRealmModel news = myNews_realm.createObject(NewsRealmModel.class);
news.setTitle(jsonObject.optString("title"));
news.setBody(jsonObject.optString("body"));
news.setUpdatedAt(jsonObject.getString("updated_at"));
ArrayList<AppImage> list = new ArrayList();
JSONArray imageArray =jsonObject.getJSONArray("appImages");
if (imageArray.length() > 1) {
for(int j=0; j<imageArray.length();j++){
appImages = new AppImage();
try {
appImages.setSrc(new JSONArray(s).getJSONObject(i).getJSONArray("appImages").getJSONObject(j).optString("src"));
}catch (JSONException e){
e.printStackTrace();
}
list.add(appImages);
}
}
news.setAppImages(list); // I am getting error for this line
TeaserImageSmall coverImage=new TeaserImageSmall();
coverImage.setSrc(new JSONArray(s).getJSONObject(i).getJSONObject("teaserImageSmall").optString("src"));
news.setTeaserImageSmall(myNews_realm.copyToRealmOrUpdate(coverImage));; //error in this line
myNews_realm.commitTransaction();
}
} catch (JSONException e) {
mListener.onFetchFailed();
}
mListener.onFetchComplete();
}
@Override
public void failure(RetrofitError error) {
Log.d(TAG, "Error :: " + error.getMessage());
mListener.onFetchComplete();
}
});
}
public interface UserCallbackListener{
void onFetchStart();
void onFetchComplete();
void onFetchFailed();
}
}
Le Logcat affiche l'erreur suivante
09-02 21:11:43.050 7337-7337/demo.app.com.bluapp_client_and E/AndroidRuntime: FATAL EXCEPTION: main
Process: demo.app.com.bluapp_client_and, PID: 7337
java.lang.IllegalArgumentException: A RealmObject with no @PrimaryKey cannot be updated: class demo.app.com.bluapp_client_and.RealmModel.TeaserImageSmall
at io.realm.Realm.checkHasPrimaryKey(Realm.java:1615)
at io.realm.Realm.copyToRealmOrUpdate(Realm.java:1030)
at demo.app.com.bluapp_client_and.controller.NewsController$1.success(NewsController.java:134)
at demo.app.com.bluapp_client_and.controller.NewsController$1.success(NewsController.java:99)
J'ai écrit cette ligne. Mais maintenant, il montre une autre erreur, j'ai mentionné dans logcat –
Edited ma réponse en conséquence. – EpicPandaForce