Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev committed Aug 23, 2019
1 parent 0026cb7 commit 77cdee2
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 407 deletions.
125 changes: 0 additions & 125 deletions Realm/Realm/AsyncOpenTask.cs

This file was deleted.

8 changes: 5 additions & 3 deletions Realm/Realm/Configurations/InMemoryConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
//
////////////////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using System.Threading.Tasks;
using Realms.Native;
using Realms.Schema;
using System;

namespace Realms
{
Expand Down Expand Up @@ -62,9 +64,9 @@ internal override Realm CreateRealm(RealmSchema schema)
return new Realm(new SharedRealmHandle(srPtr), this, schema);
}

internal override AsyncOpenTask CreateRealmAsync(RealmSchema schema)
internal override Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken)
{
return new AsyncOpenTask(CreateRealm(schema));
return Task.FromResult(CreateRealm(schema));
}
}
}
8 changes: 5 additions & 3 deletions Realm/Realm/Configurations/QueryBasedSyncConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
//
////////////////////////////////////////////////////////////////////////////

using Realms.Schema;
using System;
using System.Threading;
using System.Threading.Tasks;
using Realms.Schema;

namespace Realms.Sync
{
Expand Down Expand Up @@ -65,10 +67,10 @@ public QueryBasedSyncConfiguration(Uri serverUri = null, User user = null, strin
{
}

internal override AsyncOpenTask CreateRealmAsync(RealmSchema schema)
internal override Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken)
{
schema = RealmSchema.CreateSchemaForClasses(_queryBasedPermissionTypes, schema);
return base.CreateRealmAsync(schema);
return base.CreateRealmAsync(schema, cancellationToken);
}

internal override Realm CreateRealm(RealmSchema schema)
Expand Down
9 changes: 4 additions & 5 deletions Realm/Realm/Configurations/RealmConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,21 @@ internal override Realm CreateRealm(RealmSchema schema)
return new Realm(srHandle, this, schema);
}

internal override AsyncOpenTask CreateRealmAsync(RealmSchema schema)
internal override Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken)
{
// Can't use async/await due to mono inliner bugs
// If we are on UI thread will be set but often also set on long-lived workers to use Post back to UI thread.
if (AsyncHelper.TryGetScheduler(out var scheduler))
{
var cts = new CancellationTokenSource();
return new AsyncOpenTask(Task.Run(() =>
return Task.Run(() =>
{
using (CreateRealm(schema))
{
}
}, cts.Token).ContinueWith(_ => CreateRealm(schema), scheduler), cts);
}, cancellationToken).ContinueWith(_ => CreateRealm(schema), scheduler);
}

return new AsyncOpenTask(CreateRealm(schema));
return Task.FromResult(CreateRealm(schema));
}

[MonoPInvokeCallback(typeof(ShouldCompactCallback))]
Expand Down
6 changes: 4 additions & 2 deletions Realm/Realm/Configurations/RealmConfigurationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
//
////////////////////////////////////////////////////////////////////////////

using Realms.Schema;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Realms.Schema;

namespace Realms
{
Expand Down Expand Up @@ -140,6 +142,6 @@ internal RealmConfigurationBase Clone()

internal abstract Realm CreateRealm(RealmSchema schema);

internal abstract AsyncOpenTask CreateRealmAsync(RealmSchema schema);
internal abstract Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken);
}
}
58 changes: 49 additions & 9 deletions Realm/Realm/Configurations/SyncConfigurationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Realms.Helpers;
using Realms.Schema;
Expand Down Expand Up @@ -87,7 +88,6 @@ public abstract class SyncConfigurationBase : RealmConfigurationBase
/// progress is made during the lifetime of the Realm. It is ignored when using
/// <see cref="Realm.GetInstance(RealmConfigurationBase)"/>.
/// </summary>
[Obsolete("Use AsyncOpenTask.GetProgressObservable")]
public Action<SyncProgress> OnProgress { get; set; }

/// <summary>
Expand Down Expand Up @@ -208,7 +208,7 @@ internal override Realm CreateRealm(RealmSchema schema)
return new Realm(srHandle, this, schema);
}

internal override AsyncOpenTask CreateRealmAsync(RealmSchema schema)
internal override async Task<Realm> CreateRealmAsync(RealmSchema schema, CancellationToken cancellationToken)
{
var configuration = new Realms.Native.Configuration
{
Expand All @@ -218,16 +218,56 @@ internal override AsyncOpenTask CreateRealmAsync(RealmSchema schema)
};

var tcs = new TaskCompletionSource<ThreadSafeReferenceHandle>();
var handle = SharedRealmHandleExtensions.OpenWithSyncAsync(configuration, ToNative(), schema, EncryptionKey, tcs);
return new AsyncOpenTask(handle, tcs, (srHandle) =>
var tcsHandle = GCHandle.Alloc(tcs);
ProgressNotificationToken progressToken = null;
try
{
if (IsDynamic && !schema.Any())
using (var handle = SharedRealmHandleExtensions.OpenWithSyncAsync(configuration, ToNative(), schema, EncryptionKey, tcsHandle))
{
srHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
}
cancellationToken.Register(() =>
{
if (!handle.IsClosed)
{
handle.Cancel();
tcs.TrySetCanceled();
}
});

if (OnProgress != null)
{
progressToken = new ProgressNotificationToken(
observer: (progress) =>
{
OnProgress(progress);
},
register: handle.RegisterProgressNotifier,
unregister: (token) =>
{
if (!handle.IsClosed)
{
handle.UnregisterProgressNotifier(token);
}
});
}

return new Realm(srHandle, this, schema);
});
using (var realmReference = await tcs.Task)
{
var realmPtr = SharedRealmHandle.ResolveFromReference(realmReference);
var sharedRealmHandle = new SharedRealmHandle(realmPtr);
if (IsDynamic && !schema.Any())
{
sharedRealmHandle.GetSchema(nativeSchema => schema = RealmSchema.CreateFromObjectStoreSchema(nativeSchema));
}

return new Realm(sharedRealmHandle, this, schema);
}
}
}
finally
{
tcsHandle.Free();
progressToken?.Dispose();
}
}

internal Native.SyncConfiguration ToNative()
Expand Down
8 changes: 3 additions & 5 deletions Realm/Realm/Handles/AsyncOpenTaskHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ private static class NativeMethods
public static extern void cancel(AsyncOpenTaskHandle handle, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_async_open_task_register_progress_notifier", CallingConvention = CallingConvention.Cdecl)]
public static extern ulong register_progress_notifier(AsyncOpenTaskHandle handle,
IntPtr token_ptr,
out NativeException ex);
public static extern ulong register_progress_notifier(AsyncOpenTaskHandle handle, IntPtr token_ptr, out NativeException ex);

[DllImport(InteropConfig.DLL_NAME, EntryPoint = "realm_async_open_task_unregister_progress_notifier", CallingConvention = CallingConvention.Cdecl)]
public static extern void unregister_progress_notifier(AsyncOpenTaskHandle handle, ulong token, out NativeException ex);
Expand All @@ -55,9 +53,9 @@ protected override void Unbind()
NativeMethods.destroy(handle);
}

public ulong RegisterProgressNotifier(IntPtr tokenPtr)
public ulong RegisterProgressNotifier(GCHandle managedHandle)
{
var token = NativeMethods.register_progress_notifier(this, tokenPtr, out var ex);
var token = NativeMethods.register_progress_notifier(this, GCHandle.ToIntPtr(managedHandle), out var ex);
ex.ThrowIfNecessary();
return token;
}
Expand Down
4 changes: 2 additions & 2 deletions Realm/Realm/Handles/NotifiableObjectHandleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected NotifiableObjectHandleBase(RealmHandle root, IntPtr handle) : base(roo

public IntPtr DestroyNotificationToken(IntPtr token)
{
var result = NativeMethods.destroy_notificationtoken(token, out NativeException nativeException);
var result = NativeMethods.destroy_notificationtoken(token, out var nativeException);
nativeException.ThrowIfNecessary();
return result;
}
Expand All @@ -66,4 +66,4 @@ public IntPtr DestroyNotificationToken(IntPtr token)

public abstract ThreadSafeReferenceHandle GetThreadSafeReference();
}
}
}
6 changes: 3 additions & 3 deletions Realm/Realm/Handles/SessionHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public void RefreshAccessToken(string accessToken, string serverPath)
ex.ThrowIfNecessary();
}

public ulong RegisterProgressNotifier(IntPtr tokenPtr, ProgressDirection direction, ProgressMode mode)
public ulong RegisterProgressNotifier(GCHandle managedHandle, ProgressDirection direction, ProgressMode mode)
{
var isStreaming = mode == ProgressMode.ReportIndefinitely;
var token = NativeMethods.register_progress_notifier(this, tokenPtr, direction, isStreaming, out var ex);
var token = NativeMethods.register_progress_notifier(this, GCHandle.ToIntPtr(managedHandle), direction, isStreaming, out var ex);
ex.ThrowIfNecessary();
return token;
}
Expand Down Expand Up @@ -186,4 +186,4 @@ protected override void Unbind()
NativeMethods.destroy(handle);
}
}
}
}
Loading

0 comments on commit 77cdee2

Please sign in to comment.