我正在尝试将我的应用程序中的Realm从版本0.85更新到版本4.1.1。在某些情况下,realm.beginTransaction不返回。我试图用realm.executeTransaction替换它,但没有任何效果。以下是Realm的详细日志:
V/REALM_JNI: --> Java_io_realm_internal_OsSchemaInfo_nativeCreateFromList
V/REALM_JNI: --> Java_io_realm_internal_OsRealmConfig_nativeCreate
V/REALM_JNI: --> Java_io_realm_internal_OsRealmConfig_nativeSetInMemory -1605354496
V/REALM_JNI: --> Java_io_realm_internal_OsRealmConfig_nativeEnableChangeNotification -1605354496
V/REALM_JNI: --> Java_io_realm_internal_OsRealmConfig_nativeSetSchemaConfig -1605354496
V/REALM_JNI: --> Java_io_realm_internal_SharedRealm_nativeGetSharedRealm -1605354496
V/REALM_JNI: --> Java_io_realm_internal_SharedRealm_nativeGetSchemaInfo -1605349584
V/REALM_JNI: --> Java_io_realm_internal_SharedRealm_nativeSetAutoRefresh -1605349584
V/REALM_JNI: --> Java_io_realm_internal_SharedRealm_nativeRegisterSchemaChangedCallback -1605349584
V/REALM_JNI: --> Java_io_realm_internal_SharedRealm_nativeIsClosed -1605349584
V/REALM_JNI: --> Java_io_realm_internal_SharedRealm_nativeBeginTransaction -1605349584
因此,在我看来,Relam在调用SharedRealm.nativeBeginTransaction时卡住了,这与调试时看到的匹配。
这是我的项目build.gradle
buildscript {
repositories {
jcenter()
maven { url "https://jitpack.io" }
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.google.gms:google-services:3.1.0'
classpath ('io.realm:realm-gradle-plugin:4.1.1'){
exclude group: 'com.google.guava'
}
classpath ('com.google.firebase:firebase-plugins:1.1.0'){
exclude group: 'com.google.guava', module: 'guava-jdk5'
}
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
而我的模块build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.firebase-crash'
apply plugin: 'realm-android'
// increment code and change version name on every released build
def code = 93
def version = "1.1.8"
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode code
versionName "$version"
multiDexEnabled true
}
aaptOptions {
additionalParameters "--no-version-vectors"
}
buildTypes {
release {
buildConfigField "boolean", "DEV", "false"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
multiDexEnabled false
}
debug {
buildConfigField "boolean", "DEV", "true"
signingConfig signingConfigs.release
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:multidex:1.0.2'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:customtabs:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.google.guava:guava:23.4-android'
implementation 'uk.co.chrisjenx:calligraphy:2.2.0'
implementation 'com.makeramen:roundedimageview:2.0.1'
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
implementation 'com.google.android.gms:play-services-analytics:11.6.0'
implementation 'com.google.firebase:firebase-messaging:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
implementation 'com.google.firebase:firebase-crash:11.6.0'
implementation 'net.hockeyapp.android:HockeySDK:5.0.3'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp3:okhttp:3.9.0'
implementation 'com.squareup.okio:okio:1.13.0'
implementation 'com.squareup:otto:1.3.8'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.github.JakeWharton:ViewPagerIndicator:2.4.1'
implementation 'com.facebook.android:facebook-android-sdk:4.28.0'
}
apply plugin: 'com.google.gms.google-services'
我正在使用Android Studio 3.0,并且必须启用D8才能进行构建
对发生的事情有任何想法吗?
最佳答案
发生问题是因为realm.beginTransaction()如果被同时在不同线程上发生的写事务阻止,则不会返回。就我而言,这是由于调用RealmResults.clear()造成的。通过用deleteAllFromRealm()替换它来修复此问题。