@@ -148,7 +148,8 @@ public BoxStoreBuilder androidContext(Object context) {
148
148
if (context == null ) {
149
149
throw new NullPointerException ("Context may not be null" );
150
150
}
151
- File baseDir = new File (getAndroidFilesDir (context ), "objectbox" );
151
+ File filesDir = getAndroidFilesDir (context );
152
+ File baseDir = new File (filesDir , "objectbox" );
152
153
if (!baseDir .exists ()) {
153
154
boolean ok = baseDir .mkdirs ();
154
155
if (!ok ) {
@@ -169,12 +170,16 @@ private File getAndroidFilesDir(Object context) {
169
170
try {
170
171
Method getFilesDir = context .getClass ().getMethod ("getFilesDir" );
171
172
filesDir = (File ) getFilesDir .invoke (context );
173
+ if (filesDir == null ) {
174
+ // Race condition in Android before 4.4: https://issuetracker.google.com/issues/36918154 ?
175
+ System .err .println ("getFilesDir() returned null - retrying once..." );
176
+ filesDir = (File ) getFilesDir .invoke (context );
177
+ }
172
178
} catch (Exception e ) {
173
179
throw new RuntimeException (
174
180
"Could not init with given Android context (must be sub class of android.content.Context)" , e );
175
181
}
176
182
if (filesDir == null ) {
177
- // TODO should we consider https://issuetracker.google.com/issues/36918154 ?
178
183
throw new IllegalStateException ("Android files dir is null" );
179
184
}
180
185
return filesDir ;
@@ -190,6 +195,7 @@ private File getAndroidFilesDir(Object context) {
190
195
* For highly concurrent setups (e.g. you are using ObjectBox on the server side) it may make sense to increase the
191
196
* number.
192
197
*/
198
+
193
199
public BoxStoreBuilder maxReaders (int maxReaders ) {
194
200
this .maxReaders = maxReaders ;
195
201
return this ;
0 commit comments