我想通过Realm创建Android数据库并同步它。
这是我的build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'realm-android'
realm {
syncEnabled = true;
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.realm"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
configBuildType(delegate, "\"http://162.117.212.243:8080\"", "\"default\"", "\"myaccount_on_server@mycom.com\"", "\"my_pass\"")
}
}
}
def configBuildType(buildType, authURL, realmName, userName, password) {
println("configBuildType: buildType.name = " + buildType.name + ", authURL = " + authURL
+ ", userName = " + userName + ", password = " + password)
buildType.buildConfigField("String", "AUTH_URL", authURL)
buildType.buildConfigField("String", "REALM_NAME", realmName)
buildType.buildConfigField("String", "USER_NAME", userName)
buildType.buildConfigField("String", "PASSWORD", password)
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.google.code.gson:gson:2.8.1'
testCompile 'junit:junit:4.12'
}
所以这里是我的代码:
public class MyApplication extends Application {
private static final String TAG = MyApplication.class.getName();
private static SyncConfiguration synConfiguration;
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG)
Log.d(TAG, "onCreate:");
Realm.init(this);
RealmLog.setLevel(Log.VERBOSE);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
String authURL = BuildConfig.AUTH_URL;
SyncCredentials syncCredentials = SyncCredentials.usernamePassword(BuildConfig.USER_NAME, BuildConfig.PASSWORD, false);
SyncUser user = SyncUser.login(syncCredentials, authURL);
String realmURL = BuildConfig.AUTH_URL + "/~/" + BuildConfig.REALM_NAME;
synConfiguration = new SyncConfiguration.Builder(user, realmURL)
//.waitForInitialRemoteData()
//.readOnly()
.build();
if (BuildConfig.DEBUG)
Log.d(TAG, "onCreate: run: SUCCESS synConfiguration:\n\n" + synConfiguration + "\n\n");
} catch (Exception e) {
if (BuildConfig.DEBUG)
Log.e(TAG, "onCreate: run: error = " + e.getMessage(), e);
}
}
});
thread.start();
}
public static SyncConfiguration getSynConfiguration() {
return synConfiguration;
}
}
启动我的Android应用程序后,它成功登录,结果在logcat上我得到下一个文本:
onCreate: run: SUCCESS synConfiguration:
realmDirectory: /data/data/com.realm/files/realm-object-server/1d4e1a03e382046ea9bf7cfacc9a4ece/1d4e1a03e382046ea9bf7cfacc9a4ece
realmFileName : default
canonicalPath: /data/data/com.realm/files/realm-object-server/1d4e1a03e382046ea9bf7cfacc9a4ece/1d4e1a03e382046ea9bf7cfacc9a4ece/default
key: [length: 0]
schemaVersion: 0
migration: null
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@4222c508
readOnly: false
compactOnLaunch: null
serverUrl: realm://162.117.212.243:8080/1d4e1a03e382046ea9bf7cfacc9a4ece/default
user: {UserId: 1d4e1a03e382046ea9bf7cfacc9a4ece, AuthUrl: http://162.117.212.243:8080/auth, IsValid: true, Sessions: 0}
errorHandler: io.realm.SyncManager$1@42230f78
deleteRealmOnLogout: false
waitForInitialRemoteData: false
OK!
所以现在我想向db添加一些数据。这里代码:
public static void addOrUpdateOrganisation(final String organisationASJson) {
Realm realm = Realm.getInstance(MyApplication.getSynConfiguration());
try {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.createOrUpdateObjectFromJson(Organisation.class, organisationASJson);
}
});
} finally {
realm.close();
}
}
public class Organisation extends RealmObject {
@PrimaryKey
private long id;
@Required
private String name;
private String address;
public long getId() {
return id;
}
@Override
public String toString() {
return "\nOrganisation{" +
"id = " + id +
", name = '" + name + '\'' +
", address = '" + address + '\'' +
'}';
}
}
新的ogranisation成功增加了本地领域。但是在Realm Object Server数据(新组织)上没有显示。 为什么呢?
此处使用Realm日志进行logcat:
E/com.realm.server.server.AndroidWebServer( 3277): serve:
D/com.realm.server.server.AndroidWebServer( 3277): serve:
D/com.realm.server.server.AndroidWebServer( 3277): serve: Add/update organization...
V/REALM_JNI( 3277): --> Java_io_realm_RealmFileUserStore_nativeGetCurrentUser
V/REALM_JNI( 3277): --> Java_io_realm_RealmFileUserStore_nativeIsActive
V/REALM_JNI( 3277): --> Java_io_realm_RealmFileUserStore_nativeIsActive
E/com.realm.server.service.OrganisationService( 3277): addOrUpdateOrganisation: current user = {UserId: fccc46bbe2354bae35d3ddb9e828700c, AuthUrl: http://162.117.212.243:9080/auth, IsValid: true, Sessions: 0}
V/REALM_JNI( 3277): --> Java_io_realm_RealmFileUserStore_nativeIsActive
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCreateConfig
D/REALM_JAVA( 3277): first session created add network listener
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetSharedRealm 1473371832
D/REALM_JNI( 3277): Callback to Java requesting token for path
D/REALM_JAVA( 3277): Scheduling an access_token refresh in 280076 milliseconds
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeSetAutoRefresh 1472689096
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCloseConfig 1473371832
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeHasTable 1472689096
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetTable 1472689096
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCloseSharedRealm 1472689096
V/REALM_JNI( 3277): --> Java_io_realm_RealmFileUserStore_nativeIsActive
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCreateConfig
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetSharedRealm 1473400008
D/REALM_JNI( 3277): Callback to Java requesting token for path
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetVersion 1472884688
I/REALM_SYNC( 3277): Opening Realm file: /data/data/com.realm.server/files/realm-object-server/fccc46bbe2354bae35d3ddb9e828700c/fccc46bbe2354bae35d3ddb9e828700c/sg
D/REALM_SYNC( 3277): Connection[4]: WebSocket::Websocket()
D/REALM_SYNC( 3277): Connection[4]: Reconnecting in 0 milliseconds
I/REALM_SYNC( 3277): Connection[4]: Session[12]: Starting session for '/data/data/com.realm.server/files/realm-object-server/fccc46bbe2354bae35d3ddb9e828700c/fccc46bbe2354bae35d3ddb9e828700c/sg'
D/REALM_SYNC( 3277): Connection[4]: Session[12]: last_version_available = 8
D/REALM_SYNC( 3277): Connection[4]: Session[12]: progress_server_version = 0
D/REALM_SYNC( 3277): Connection[4]: Session[12]: progress_client_version = 0
D/REALM_SYNC( 3277): Using already open Realm file: /data/data/com.realm.server/files/realm-object-server/fccc46bbe2354bae35d3ddb9e828700c/fccc46bbe2354bae35d3ddb9e828700c/sg
D/REALM_SYNC( 3277): Connection[4]: Session[12]: Progress handler called, downloaded = 0, downloadable = 0, uploaded = 0, uploadable = 258, progress version = 0, snapshot version = 8
I/REALM_SYNC( 3277): Connection[4]: Resolving '162.117.212.243:9080'
I/REALM_SYNC( 3277): Connection[4]: Connecting to endpoint '162.117.212.243:9080' (1/1)
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeSetAutoRefresh 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCloseConfig 1473400008
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetVersion 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeBeginTransaction 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetVersion 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetVersion 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_OsSchemaInfo_nativeCreateFromList
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeUpdateSchema 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeHasTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeHasTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_UncheckedRow_nativeGetString 1472887608
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCommitTransaction 1472884688
I/REALM_SYNC( 3277): Connection[4]: Connected to endpoint '162.117.212.243:9080' (from '172.27.202.74:39542')
D/REALM_SYNC( 3277): Connection[4]: WebSocket::initiate_client_handshake()
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeBeginTransaction 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetVersion 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeHasTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeGetTable 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_UncheckedRow_nativeGetString 1472888688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsInTransaction 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_UncheckedRow_nativeGetIndex 1473409440
V/REALM_JNI( 3277): --> Java_io_realm_internal_UncheckedRow_nativeSetString 1473409440
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsInTransaction 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_UncheckedRow_nativeGetIndex 1473409440
V/REALM_JNI( 3277): --> Java_io_realm_internal_UncheckedRow_nativeSetString 1473409440
D/com.realm.server.service.OrganisationService( 3277): executeTransaction: execute: SUCCESS created/updated organisation:
D/com.realm.server.service.OrganisationService( 3277): {
D/com.realm.server.service.OrganisationService( 3277): "id": 1,
D/com.realm.server.service.OrganisationService( 3277): "address": "Address 1",
D/com.realm.server.service.OrganisationService( 3277): "name": "Ogranisation 1"
D/com.realm.server.service.OrganisationService( 3277): }
V/REALM_SYNC( 3277): Connection[4]: HTTP request =
V/REALM_SYNC( 3277): GET /realm-object-server HTTP/1.1
V/REALM_SYNC( 3277): Connection: Upgrade
V/REALM_SYNC( 3277): Host: 162.117.212.243
V/REALM_SYNC( 3277): Sec-WebSocket-Key: 3wrQNa93e9Cs6MyN+meAVQ==
V/REALM_SYNC( 3277): Sec-WebSocket-Protocol: io.realm.protocol
V/REALM_SYNC( 3277): Sec-WebSocket-Version: 13
V/REALM_SYNC( 3277): Upgrade: websocket
V/REALM_SYNC( 3277): X-Realm-Access-Token: eyJhY2Nlc3MiOlsiZG93bmxvYWQiLCJ1cGxvYWQiLCJtYW5hZ2UiXSwiYXBwX2lkIjoibWQucmVhbG0uc2VydmVyIiwiZXhwaXJlcyI6MTUwMjE4NDQ3MCwiaWQiOiJkMTQ1YjM0NmJkMjM0N2M3OTFhNGVhMTIxNDBlY2M4OSIsImlkZW50aXR5IjoiZmNjYzQ2YmJlMjM1NGJhZTM1ZDNkZGI5ZTgyODcwMGMiLCJwYXRoIjoiL2ZjY2M0NmJiZTIzNTRiYWUzNWQzZGRiOWU4Mjg3MDBjL3NnIn0=:OdHN3vi05miZns6mUPPZIKFE+U0kp6UWRMDgOA6HSRdsI5yRDY8QsOI8fzckNGzN6iZc9dhoLNh4Nwwx520CtID39VVYNIpGrRSGhgKBUqnNAfk7vz0BaU4F3d4nSaOAG80QX9v0PUB0ZbzuoIGFYP2y1hipPTwPYbcir9Rwe/YbI1VKhLSAF2sARdNHUyuaedV/wIzXC51DNAQvDhQ8KcdVUyC2/+7j1nBg2jR1G0IbKxcCbeNE8kcatZYGnZzcxxaMJwyr3MUEdwtVXFRLKZXHaZy4GQXZx4/rd6MGM8+hzMgwOV5P4LUTfApWzAjystHWkFPJ2qnDyvz9BLP3OQ==
V/REALM_SYNC( 3277): X-Realm-Path: /fccc46bbe2354bae35d3ddb9e828700c/sg
V/REALM_SYNC( 3277): X-Realm-Protocol-Version: 18
V/REALM_SYNC( 3277):
D/REALM_SYNC( 3277): Using already open Realm file: /data/data/com.realm.server/files/realm-object-server/fccc46bbe2354bae35d3ddb9e828700c/fccc46bbe2354bae35d3ddb9e828700c/sg
D/REALM_SYNC( 3277): Connection[4]: Session[12]: Progress handler called, downloaded = 0, downloadable = 0, uploaded = 0, uploadable = 258, progress version = 0, snapshot version = 9
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeIsClosed 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCommitTransaction 1472884688
V/REALM_JNI( 3277): --> Java_io_realm_internal_SharedRealm_nativeCloseSharedRealm 1472884688
D/REALM_JAVA( 3277): last session dropped, remove network listener
D/com.realm.server.server.AndroidWebServer( 3277): serve: RESULT RESPONSE:
D/com.realm.server.server.AndroidWebServer( 3277): {Success}
I/REALM_SYNC( 3277): Closing Realm file: /data/data/com.realm.server/files/realm-object-server/fccc46bbe2354bae35d3ddb9e828700c/fccc46bbe2354bae35d3ddb9e828700c/sg
D/dalvikvm( 7361): GC_CONCURRENT freed 388K, 5% free 9124K/9568K, paused 3ms+3ms, total 27ms
答案 0 :(得分:0)
如评论中所述,同步仅在Realm实例打开时发生,因此很可能您在完成上传所有更改之前关闭了Realm。
您可以使用“同步进度通知”了解何时上传所有更改。请参阅此处文档中的部分:https://realm.io/docs/java/latest/#sync-sessions
ProgressListener listener = new ProgressListener() {
@Override
public void onChange(Progress progress) {
if (progress.isTransferComplete()) {
// Realm can be safely closed now
// Remember that this callback happens on a different thread
// than where the Realm live.
}
}
};
SyncSession session = SyncManager.getSession(getSyncConfiguration());
session.addUploadProgressListener(ProgressMode.CURRENT_CHANGES, listener);