Skip to content

sushihangover/Realm.Json.Extensions

Repository files navigation

SushiHangover.RealmJson.Extensions

Extension Methods for adding JSON APIs to a Realm Instance

Current project status:

Build status

Nuget:

PM> Install-Package RealmJson.Extensions

Ref: https://www.nuget.org/packages/RealmJson.Extensions

Issues?

###Post issues on GitHub

Need Help?

Post on StackOverflow with the tags: [XAMARIN] [REALM] [JSON]

API Reference:

https://sushihangover.github.io/Realm.Json.Extensions/

Extension API:

  • A Realm Instance:

    • .CreateAllFromJson<T>(string)
    • .CreateAllFromJson<T>(Stream)
    • .CreateAllFromJsonViaAutoMapper<T>(Stream)
    • .CreateObjectFromJson<T>(string)
    • .CreateObjectFromJson<T>(Stream)
    • .CreateOrUpdateObjectFromJson<T>(string)
    • .CreateOrUpdateObjectFromJson<T>(Stream)
    • .CreateOrUpdateObjectFromJson<T>(Stream)
  • A RealmObject Instance:

    • .NonManagedCopy<T>()
  • A RealmResult Instance (IQueryable):

    • .NonManagedCopy

Github Repo:

https://github.com/sushihangover/Realm.Json.Extensions

Usage / Examples:

Copy a managed object to a non-managed instacne:

var syncedObject = realmObject.NonManagedCopy<ARealmClass>();

Re: https://stackoverflow.com/a/46629763/4984832

Single RealmObject from Json-based string:

using (var theRealm = Realm.GetInstance(RealmDBTempPath()))
{
	var realmObject = theRealm.CreateObjectFromJson<StateUnique>(jsonString);
}

Single RealmObject from a Stream:

using (var stream = new MemoryStream(byteArray))
using (var theRealm = Realm.GetInstance(RealmDBTempPath()))
{
	var testObject = theRealm.CreateObjectFromJson<StateUnique>(stream);
}

Using Json Arrays from a Android Asset Stream:

using (var theRealm = Realm.GetInstance(RealmDBTempPath()))
using (var assetStream = Application.Context.Assets.Open("States.json"))
{
	theRealm.CreateAllFromJson<State>(assetStream);
}

Using Json Arrays from a iOS Bundled Resource Stream:

using (var theRealm = Realm.GetInstance(RealmDBTempPath()))
using (var fileStream = new FileStream("./Data/States.json", FileMode.Open, FileAccess.Read))
{
	theRealm.CreateAllFromJson<State>(fileStream);
}

Note: Consult the Unit Tests for more usage details

(1)Xamarin.Forms Usage

AutoMapper does not support PCL Profile 259(2)) and thus adding this Nuget package will fail if applied to a default Xamarin.Form project.

You can change your Xamarin.From project to Profile 111, then retarget the Xamarin.Forms package, and you will be able to add the Nuget package.

See the Xamarin.Forms-based (Nuget.Test) project in the code repo as an example.

Note: Once Xamarin has full support for .NET Standard and AutoMapper releases v5.2 (3), this mess should go away.

(2) AutoMapper/AutoMapper#1531

(3) AutoMapper/AutoMapper#1532

##Build Documention:

API Reference documention is built via the great doxygen

doxygen Doxygen/realmthread.config

Building:

xbuild or msbuild based:

xbuild /p:SolutionDir=./ /target:Clean /p:Configuration=Release   RealmJson.Extensions/RealmJson.Extensions.csproj
xbuild /p:SolutionDir=./ /target:Build /p:Configuration=Release RealmJson.Extensions/RealmJson.Extensions.csproj

Testing / NUnitLite Automation:

Xamarin.Android

adb shell am instrument -w RealmJson.Test.Droid/app.tests.TestInstrumentation

Ref: Automate Android Nunit Test

Ref: Issue 32005

Xamarin.iOS

xbuild RealmJson.Test.iOS/RealmJson.Test.iOS.csproj /p:SolutionDir=~/
xcrun simctl list
open -a Simulator --args -CurrentDeviceUDID 360D3BC6-4A6D-4B7E-A899-5C7651EC2107
xcrun simctl install booted  ./RealmJson.Test.iOS/bin/iPhoneSimulator/Debug/RealmJson.Test.iOS.app
xcrun simctl launch booted com.sushihangover.realmjson-test-ios
cat ~/Library/Logs/CoreSimulator/360D3BC6-4A6D-4B7E-A899-5C7651EC2107/system.log

Performance Testing:

There is a #if PERF within Tests.Shared.Perf.Streaming.cs that contains multple tests to evaluate streaming performance and memory overhead.

These test methods try to evaluate:

  1. Realm.Manage vs. AutoMapper performance/effeciency
  2. Upper limits of the number of RealmObjects can be used in a Realm Transaction on a mobile device.

See Perf.md for details.

License

Consult LICENSE