Skip to content

Instantly share code, notes, and snippets.

View cmelchior's full-sized avatar

Christian Melchior cmelchior

View GitHub Profile
/**
* Class connecting the Realm lifecycle to that of LiveData objects.
* Realm will remain open for as long as any LiveData objects are being observed.
*/
abstract class LiveRealmData<T: RealmModel>(val config: RealmConfiguration) : LiveData<RealmResults<T>>() {
private val listener = RealmChangeListener<RealmResults<T>> { results -> value = results }
private lateinit var realm: Realm
private var results: RealmResults<T>? = null
@cmelchior
cmelchior / PrimaryKeyFactory.java
Created April 7, 2017 12:01
Helper class for creating auto increment keys for Realm model classes
/*
* Copyright 2017 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@cmelchior
cmelchior / LogCat
Last active April 3, 2017 19:48
Debugging Progress Listeners
04-03 15:44:04.233 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 0, is_fresh 0
04-03 15:44:04.237 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 0, is_fresh 0
04-03 15:44:04.276 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 8844, is_fresh 0
04-03 15:44:04.279 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 9187, is_fresh 0
04-03 15:44:04.289 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,upload
@cmelchior
cmelchior / HelperMethods.java
Created March 17, 2017 10:54
Load new Realm file from assets when a migration is required
public Realm loadAssetFileOnMigration() {
// TODO Don't re-create this every time
RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(2) // Bumping the schema because new app
.build();
Realm realm;
try {
realm = Realm.getInstance(config);
@cmelchior
cmelchior / github_issue_rankings.rb
Last active March 7, 2022 18:58
Small ruby script that will rank all Github issues in a repo according to which is the most popular
#!/usr/bin/ruby -w
#
# This script will analyze a Github repo and try to rank all open issues with regard to popularity.
# WARNING: This script will run quite a few HTTP requests against Github to do this analysis. At least 1 pr. issue.
# The current limit on Github is 5000 requests pr. hour: https://developer.github.com/v3/rate_limit/
#
# Usage: ./ruby github-issue-rankings.rb <github_user/repo> <github_api_access_token>
# Example: ruby github_issue_rankings.rb realm/realm-java $GITHUB_ISSUE_RANKINGS_ACCESS_TOKEN
#
# The algorithm for ranking issues are the following:
@cmelchior
cmelchior / LoggingAstVisitor.java
Created May 4, 2016 06:31
Helper class for writing Lint Checks for Android
/*
* Copyright 2016 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@cmelchior
cmelchior / Example.java
Created May 3, 2016 15:10
Example of using Realm.waitForChange()/Realm.stopWaitForChange()
// Create a background thread
RealmThread t = new RealmThread(realmConfig, new RealmRunnable() {
@Override
public void run(Realm realm) {
do {
// Do some work once, and every time the Realm changes
} while (realm.waitForChange())
}
});
@cmelchior
cmelchior / BarListParcelConverter.java
Last active August 29, 2019 09:46
Using Parceler 1.0.3 with Realm
// Specific class for a RealmList<Bar> field
public class BarListParcelConverter extends RealmListParcelConverter<Bar> {
@Override
public void itemToParcel(Bar input, Parcel parcel) {
parcel.writeParcelable(Parcels.wrap(input), 0);
}
@Override
public Bar itemFromParcel(Parcel parcel) {
@cmelchior
cmelchior / SerializeToJson.java
Last active September 20, 2022 04:30
Serialize Realm objects to JSON using GSON
// GSON can parse the data.
//
// Deserialization:
// Note there is a bug in GSON 2.3.1 that can cause it to StackOverflow when working with RealmObjects.
// To work around this, use the ExclusionStrategy below or downgrade to 1.7.1
// See more here: https://code.google.com/p/google-gson/issues/detail?id=440
//
// Serialization:
// <Type>RealmProxy objects are created by the Realm annotation processor. They are used to control
// access to the actual data instead of storing them in fields and it is therefore them we need to register a
@cmelchior
cmelchior / CustomTypeAdapter.java
Created April 9, 2015 06:35
Realm, GSON and primitive JSON arrays
// Make a custom Gson instance, with a custom TypeAdapter for each wrapper object.
// In this instance we only have RealmList<RealmInt> as a a wrapper for RealmList<Integer>
Type token = new TypeToken<RealmList<RealmInt>>(){}.getType();
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}