I know it sounds crazy and I sound stupid, but I tested it like 20times now and i have no other explanation about it, maybe I am doing something very wrong or I don't know.
Ok I am creating a widget, that uses Realm Database to load data from. I have the Realm default configuration declared on Application level:
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
Then I add some info to the Realm in the MainActivity. I have widget Provider that run RemoteViewsService to load the data from the realm and put it in the RemoteViewsFactory. Here is the RemoteViewsService, where the interesting part is:
public class MyWidService extends RemoteViewsService {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public MyWidFactory onGetViewFactory(Intent intent) {
Log.d("MyWidService", " onGetViewFactory()<<<<<<<<<<");
final Context context = getApplicationContext();
Realm.init(context);
Realm realm = Realm.getDefaultInstance();
realm.isAutoRefresh();
ArrayList<String> myList = new ArrayList<>();
RealmResults<Task> myResults = realm.where(Task.class).findAll();
final int myResLength = myResults.size();
Log.d("myResLength", " "+ myResLength);
for(int j = 0; j<myResLength; j++){
myList.add(myResults.get(j).getTaskText());
Log.d("tasks", " " + myList.get(j));
}
return new MyWidFactory(context, intent, myList);
}
}
So you see that Log.d("myResLength", " "+ myResLength); ? I dont know why but the widget simply shows that there is no data loaded without it. What is more, without the Log.d, Android Monitor show that the compiler simpy skips the whole for cycle after it. I don't know if this is Android bug, Realm bug or my ignorance, but thats the only way that the data loads on the widget. Now that seems unreliable option to me so I need some explanation about it.