Skip to content

Commit

Permalink
Speed-up calls to Realm.Manage (#857)
Browse files Browse the repository at this point in the history
Weave in a CopyToRealm to IRealmObjectHelper
  • Loading branch information
nirinchev authored Oct 7, 2016
1 parent 65dcbf2 commit 6eee401
Show file tree
Hide file tree
Showing 19 changed files with 661 additions and 135 deletions.
3 changes: 0 additions & 3 deletions Realm.PCL/Realm.PCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@
<Link>PreserveAttribute.cs</Link>
</Compile>
<Compile Include="Extensions\RealmResultsCollectionChangedPCL.cs" />
<Compile Include="..\Realm.Shared\ICopyValuesFrom.cs">
<Link>ICopyValuesFrom.cs</Link>
</Compile>
<Compile Include="..\Realm.Shared\exceptions\RealmInvalidObjectException.cs">
<Link>Exceptions\RealmInvalidObjectException.cs</Link>
</Compile>
Expand Down
7 changes: 1 addition & 6 deletions Realm.PCL/RealmListPCL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Realms
/// </remarks>
///
/// <typeparam name="T">Type of the RealmObject which is the target of the relationship.</typeparam>
public class RealmList<T> : IList<T>, IRealmList, IDynamicMetaObjectProvider, ICopyValuesFrom where T : RealmObject
public class RealmList<T> : IList<T>, IRealmList, IDynamicMetaObjectProvider where T : RealmObject
{

/// <summary>
Expand Down Expand Up @@ -227,11 +227,6 @@ private void ManageObjectIfNeeded(T obj)

#endregion

void ICopyValuesFrom.CopyValuesFrom(IEnumerable<RealmObject> values)
{
RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
}

DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
{
RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();
Expand Down
6 changes: 0 additions & 6 deletions Realm.Shared/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,5 @@ public WovenAttribute(Type helperType)
[AttributeUsage(AttributeTargets.Property)]
public class WovenPropertyAttribute : Attribute
{
internal string BackingFieldName { get; private set; }

public WovenPropertyAttribute(string backingFieldName)
{
this.BackingFieldName = backingFieldName;
}
}
}
7 changes: 7 additions & 0 deletions Realm.Shared/Dynamic/DynamicRealmObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Reflection;
using Realms.Weaving;

namespace Realms.Dynamic
Expand All @@ -25,6 +27,11 @@ internal class DynamicRealmObjectHelper : IRealmObjectHelper
{
internal static readonly DynamicRealmObjectHelper Instance = new DynamicRealmObjectHelper();

public void CopyToRealm(RealmObject instance)
{
throw new NotSupportedException("DynamicRealmObjectHelper cannot exist in unmanaged state, so CopyToRealm should not be called ever.");
}

public RealmObject CreateInstance()
{
return new DynamicRealmObject();
Expand Down
11 changes: 0 additions & 11 deletions Realm.Shared/ICopyValuesFrom.cs

This file was deleted.

1 change: 0 additions & 1 deletion Realm.Shared/Realm.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<Compile Include="$(MSBuildThisFileDirectory)handles\NotificationTokenHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)exceptions\RealmInvalidTransactionException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)extensions\RealmResultsCollectionChanged.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ICopyValuesFrom.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Schema\RealmSchema.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Schema\PropertyType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Schema\Property.cs" />
Expand Down
10 changes: 1 addition & 9 deletions Realm.Shared/RealmList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Realms
///
/// <typeparam name="T">Type of the RealmObject which is the target of the relationship.</typeparam>
[Preserve(AllMembers = true)]
public class RealmList<T> : IList<T>, IRealmList, IDynamicMetaObjectProvider, ICopyValuesFrom where T : RealmObject
public class RealmList<T> : IList<T>, IRealmList, IDynamicMetaObjectProvider where T : RealmObject
{
public class Enumerator : IEnumerator<T>
{
Expand Down Expand Up @@ -312,14 +312,6 @@ private void ManageObjectIfNeeded(T obj)
#endregion

DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression expression) => new Dynamic.MetaRealmList(expression, this);

void ICopyValuesFrom.CopyValuesFrom(IEnumerable<RealmObject> values)
{
foreach (var item in values.Cast<T>())
{
Add(item);
}
}
}

[Preserve(AllMembers = true)]
Expand Down
23 changes: 1 addition & 22 deletions Realm.Shared/RealmObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,7 @@ internal class Metadata
internal void _CopyDataFromBackingFieldsToRow()
{
Debug.Assert(this.IsManaged);

foreach (var property in _metadata.Schema)
{
var field = property.PropertyInfo.DeclaringType.GetField(
property.PropertyInfo.GetCustomAttribute<WovenPropertyAttribute>().BackingFieldName,
BindingFlags.Instance | BindingFlags.NonPublic
);
var value = field?.GetValue(this);
if (value != null) {
var listValue = value as IEnumerable<RealmObject>;
if (listValue != null) // assume it is IList NOT a RealmList so need to wipe afer copy
{
// cope with ReplaceListGetter creating a getter which assumes
// a backing field for a managed IList is already a RealmList, so null it first
field.SetValue(this, null); // now getter will create a RealmList below
var realmList = (ICopyValuesFrom)property.PropertyInfo.GetValue(this, null);
realmList.CopyValuesFrom(listValue);
} else {
property.PropertyInfo.SetValue(this, value, null);
}
} // only null if blank relationship or string so leave as default
}
_metadata.Helper.CopyToRealm(this);
}

#region Getters
Expand Down
7 changes: 3 additions & 4 deletions Realm.Shared/weaving/IRealmObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
//
////////////////////////////////////////////////////////////////////////////

using System;

namespace Realms.Weaving
{
public interface IRealmObjectHelper
{
RealmObject CreateInstance();
}
}

void CopyToRealm(RealmObject instance);
}
}
Loading

0 comments on commit 6eee401

Please sign in to comment.