Skip to content

Commit

Permalink
Realm Object Serverにつなぐコード
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuke Iwaki committed Dec 4, 2016
1 parent bc3d205 commit e94b3cd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Expand Up @@ -19,6 +19,10 @@ android {
}
}

realm {
syncEnabled = true;
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
Expand Down
@@ -1,11 +1,16 @@
package io.github.yusukeiwaki.realmdebugsample;

import android.app.Application;
import android.util.Log;

import com.jakewharton.threetenabp.AndroidThreeTen;

import io.realm.ObjectServerError;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.SyncConfiguration;
import io.realm.SyncCredentials;
import io.realm.SyncUser;

public class RealmDebugSampleApplication extends Application {
@Override
Expand All @@ -15,9 +20,19 @@ public void onCreate() {
AndroidThreeTen.init(this);

Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
SyncCredentials credentials = SyncCredentials.usernamePassword(
"yusuke.iwaki@example.com", "hogehoge", false);
SyncUser.loginAsync(credentials, "http://10.0.3.2:9080/auth", new SyncUser.Callback() {
@Override
public void onSuccess(SyncUser user) {
RealmConfiguration config = new SyncConfiguration.Builder(user, "realm://10.0.3.2:9080/~/debug").build();
Realm.setDefaultConfiguration(config);
}

@Override
public void onError(ObjectServerError error) {
Log.e("RealmDebugSample", "error", error);
}
});
}
}
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;

import io.github.yusukeiwaki.realmdebugsample.service.observer.SendMessageObserver;
import io.realm.Realm;

public class BackgroundSyncService extends Service {

Expand All @@ -25,6 +26,12 @@ public void onCreate() {

setupListeners();
registerListeners();

try (Realm realm = Realm.getDefaultInstance()) {
if (!"debug".equals(realm.getConfiguration().getRealmFileName())) {
stopSelf();
}
}
}

@Override
Expand Down

0 comments on commit e94b3cd

Please sign in to comment.