Created
May 24, 2017 18:31
-
-
Save apexJCL/db57d140c573d311131cc33050113c3f to your computer and use it in GitHub Desktop.
Realm Sync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ROS_CONTACTS_REALM_URL = realm://192.168.1.98:9080/lynxenger/contacts | |
public static void initContactsRealm(final SyncUser user, final String username) { | |
if (!user.isAdmin()) | |
return; | |
user.getManagementRealm().executeTransaction(new Realm.Transaction() { | |
@Override | |
public void execute(Realm realm) { | |
Boolean mayRead = true; | |
Boolean mayWrite = true; | |
Boolean mayManage = true; | |
PermissionChange change = new PermissionChange(LynxengerApplication.ROS_CONTACTS_REALM_URL, "*", mayRead, mayWrite, mayManage); | |
realm.insert(change); | |
} | |
}); | |
SyncConfiguration configuration = new SyncConfiguration.Builder(user, LynxengerApplication.ROS_CONTACTS_REALM_URL).schemaVersion(BuildConfig.SCHEMA_VERSION).build(); | |
Realm r = Realm.getInstance(configuration); // Retrieves remote contacts realm | |
long count = r.where(Contact.class).equalTo("userId", user.getIdentity()).count(); | |
if (count == 0) | |
r.executeTransactionAsync(new Realm.Transaction() { | |
@Override | |
public void execute(Realm realm) { | |
Contact c = realm.createObject(Contact.class); | |
c.setUserId(user.getIdentity()); | |
c.setCreatedOn(new Date()); | |
c.setUsername(username); | |
c.setLastLoggedIn(new Date()); | |
} | |
}); | |
r.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small point (unrelated to permission bug) but looking over this code, the query/count check is unsafe since the downloading of the data from the Realm occurs asynchronously vs. the query will be applied synchronously.