From 9d3c9dc1785aa916a472239288ab36d4b1ecac7c Mon Sep 17 00:00:00 2001 From: oneRain Date: Wed, 28 Aug 2019 11:07:12 +0800 Subject: [PATCH] =?UTF-8?q?*=20AVLiveQuery.cs:=20chore:=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20Object=20Fetch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * AVFile.cs: * AVCloud.cs: * AVObject.cs: * AVExtensions.cs: * ObjectControllerTests.cs: --- LiveQuery/Source/AVLiveQuery.cs | 4 +- Storage/Storage.Test/ObjectControllerTests.cs | 22 +++++- Storage/Storage/Public/AVCloud.cs | 4 +- Storage/Storage/Public/AVExtensions.cs | 77 ++++++------------- Storage/Storage/Public/AVFile.cs | 2 +- Storage/Storage/Public/AVObject.cs | 6 +- 6 files changed, 52 insertions(+), 63 deletions(-) diff --git a/LiveQuery/Source/AVLiveQuery.cs b/LiveQuery/Source/AVLiveQuery.cs index f680ec4..b414d4d 100644 --- a/LiveQuery/Source/AVLiveQuery.cs +++ b/LiveQuery/Source/AVLiveQuery.cs @@ -179,7 +179,7 @@ namespace LeanCloud.LiveQuery { "id", AVLiveQuery.InstallationId }, { "clientTimestamp", AVLiveQuery.ClientTs } }; - string sessionToken = AVUser.CurrentUser != null ? AVUser.CurrentUser.SessionToken : string.Empty; + string sessionToken = AVUser.CurrentUser?.SessionToken; if (!string.IsNullOrEmpty(sessionToken)) { data.Add("sessionToken", sessionToken); } @@ -200,7 +200,7 @@ namespace LeanCloud.LiveQuery { "id", AVLiveQuery.InstallationId }, { "query_id", Id }, }; - string sessionToken = AVUser.CurrentUser != null ? AVUser.CurrentUser.SessionToken : string.Empty; + string sessionToken = AVUser.CurrentUser?.SessionToken; var command = new AVCommand("LiveQuery/unsubscribe", "POST", sessionToken, diff --git a/Storage/Storage.Test/ObjectControllerTests.cs b/Storage/Storage.Test/ObjectControllerTests.cs index 4f344d0..5b02f5f 100644 --- a/Storage/Storage.Test/ObjectControllerTests.cs +++ b/Storage/Storage.Test/ObjectControllerTests.cs @@ -1,6 +1,7 @@ using NUnit.Framework; using System.Threading; using System.Threading.Tasks; +using System.Collections.Generic; using LeanCloud; namespace LeanCloudTests { @@ -11,9 +12,9 @@ namespace LeanCloudTests { } [Test] - public async Task TestSave() { + public async Task Save() { TestContext.Out.WriteLine($"before at {Thread.CurrentThread.ManagedThreadId}"); - var obj = AVObject.Create("Foo"); + AVObject obj = AVObject.Create("Foo"); obj["content"] = "hello, world"; await obj.SaveAsync(); TestContext.Out.WriteLine($"{obj.ObjectId} saved at {Thread.CurrentThread.ManagedThreadId}"); @@ -23,12 +24,27 @@ namespace LeanCloudTests { } [Test] - public async Task ObjectFetch() { + public async Task Fetch() { AVObject obj = AVObject.CreateWithoutData("Todo", "5d5f6039d5de2b006cf29c8f"); await obj.FetchAsync(); Assert.NotNull(obj["title"]); Assert.NotNull(obj["content"]); TestContext.Out.WriteLine($"{obj["title"]}, {obj["content"]}"); } + + [Test] + public async Task FetchWithKeys() { + AVObject obj = AVObject.CreateWithoutData("Post", "5d3abfa530863b0068e1b326"); + await obj.FetchAsync(new List { "pubUser" }); + TestContext.Out.WriteLine($"{obj["pubUser"]}"); + } + + [Test] + public async Task FetchWithIncludes() { + AVObject obj = AVObject.CreateWithoutData("Post", "5d3abfa530863b0068e1b326"); + await obj.FetchAsync(includes: new List { "tag" }); + AVObject tag = obj["tag"] as AVObject; + TestContext.Out.WriteLine($"{tag["name"]}"); + } } } diff --git a/Storage/Storage/Public/AVCloud.cs b/Storage/Storage/Public/AVCloud.cs index 9dddf7d..5d4d403 100644 --- a/Storage/Storage/Public/AVCloud.cs +++ b/Storage/Storage/Public/AVCloud.cs @@ -43,7 +43,7 @@ namespace LeanCloud { /// The result of the cloud call. public static Task CallFunctionAsync(String name, IDictionary parameters = null, string sesstionToken = null, CancellationToken cancellationToken = default(CancellationToken)) { return CloudCodeController.CallFunctionAsync(name, - parameters, AVUser.CurrentUser.SessionToken, + parameters, AVUser.CurrentUser?.SessionToken, cancellationToken); } @@ -58,7 +58,7 @@ namespace LeanCloud { /// public static Task RPCFunctionAsync(String name, IDictionary parameters = null, string sesstionToken = null, CancellationToken cancellationToken = default(CancellationToken)) { return CloudCodeController.RPCFunction(name, - parameters, AVUser.CurrentUser.SessionToken, + parameters, AVUser.CurrentUser?.SessionToken, cancellationToken); } diff --git a/Storage/Storage/Public/AVExtensions.cs b/Storage/Storage/Public/AVExtensions.cs index 973ff5c..cb8ea1a 100644 --- a/Storage/Storage/Public/AVExtensions.cs +++ b/Storage/Storage/Public/AVExtensions.cs @@ -1,24 +1,22 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Linq; using LeanCloud.Storage.Internal; -namespace LeanCloud -{ +namespace LeanCloud { /// /// Provides convenience extension methods for working with collections /// of AVObjects so that you can easily save and fetch them in batches. /// - public static class AVExtensions - { + public static class AVExtensions { /// /// Saves all of the AVObjects in the enumeration. Equivalent to /// calling . /// /// The objects to save. - public static Task SaveAllAsync(this IEnumerable objects) where T : AVObject - { + public static Task SaveAllAsync(this IEnumerable objects) where T : AVObject { return AVObject.SaveAllAsync(objects); } @@ -30,8 +28,7 @@ namespace LeanCloud /// The objects to save. /// The cancellation token. public static Task SaveAllAsync( - this IEnumerable objects, CancellationToken cancellationToken) where T : AVObject - { + this IEnumerable objects, CancellationToken cancellationToken) where T : AVObject { return AVObject.SaveAllAsync(objects, cancellationToken); } @@ -41,8 +38,7 @@ namespace LeanCloud /// /// The objects to save. public static Task> FetchAllAsync(this IEnumerable objects) - where T : AVObject - { + where T : AVObject { return AVObject.FetchAllAsync(objects); } @@ -55,8 +51,7 @@ namespace LeanCloud /// The cancellation token. public static Task> FetchAllAsync( this IEnumerable objects, CancellationToken cancellationToken) - where T : AVObject - { + where T : AVObject { return AVObject.FetchAllAsync(objects, cancellationToken); } @@ -68,8 +63,7 @@ namespace LeanCloud /// The objects to fetch. public static Task> FetchAllIfNeededAsync( this IEnumerable objects) - where T : AVObject - { + where T : AVObject { return AVObject.FetchAllIfNeededAsync(objects); } @@ -82,8 +76,7 @@ namespace LeanCloud /// The cancellation token. public static Task> FetchAllIfNeededAsync( this IEnumerable objects, CancellationToken cancellationToken) - where T : AVObject - { + where T : AVObject { return AVObject.FetchAllIfNeededAsync(objects, cancellationToken); } @@ -95,43 +88,25 @@ namespace LeanCloud /// The list of AVQueries to 'or' together. /// A query that is the or of the given queries. public static AVQuery Or(this AVQuery source, params AVQuery[] queries) - where T : AVObject - { + where T : AVObject { return AVQuery.Or(queries.Concat(new[] { source })); } - /// - /// Fetches this object with the data from the server. - /// - public static Task FetchAsync(this T obj) where T : AVObject - { - return obj.FetchAsyncInternal(CancellationToken.None).OnSuccess(t => (T)t.Result); - } - - /// - /// Fetches this object with the data from the server. - /// - /// The AVObject to fetch. - /// The cancellation token. - public static Task FetchAsync(this T obj, CancellationToken cancellationToken) - where T : AVObject - { - return FetchAsync(obj, null, cancellationToken); - } - public static Task FetchAsync(this T obj, IEnumerable includeKeys) where T : AVObject - { - return FetchAsync(obj, includeKeys, CancellationToken.None).OnSuccess(t => (T)t.Result); - } - public static Task FetchAsync(this T obj, IEnumerable includeKeys, CancellationToken cancellationToken) - where T : AVObject - { + public static Task FetchAsync(this T obj, + IEnumerable keys = null, IEnumerable includes = null, AVACL includeACL = null, + CancellationToken cancellationToken = default) where T : AVObject { var queryString = new Dictionary(); - if (includeKeys != null) - { - - var encode = string.Join(",", includeKeys.ToArray()); + if (keys != null) { + var encode = String.Join(",", keys.ToArray()); + queryString.Add("keys", encode); + } + if (includes != null) { + var encode = String.Join(",", includes.ToArray()); queryString.Add("include", encode); } + if (includeACL != null) { + queryString.Add("returnACL", includeACL); + } return obj.FetchAsyncInternal(queryString, cancellationToken).OnSuccess(t => (T)t.Result); } @@ -140,8 +115,7 @@ namespace LeanCloud /// false), fetches this object with the data from the server. /// /// The AVObject to fetch. - public static Task FetchIfNeededAsync(this T obj) where T : AVObject - { + public static Task FetchIfNeededAsync(this T obj) where T : AVObject { return obj.FetchIfNeededAsyncInternal(CancellationToken.None).OnSuccess(t => (T)t.Result); } @@ -152,8 +126,7 @@ namespace LeanCloud /// The AVObject to fetch. /// The cancellation token. public static Task FetchIfNeededAsync(this T obj, CancellationToken cancellationToken) - where T : AVObject - { + where T : AVObject { return obj.FetchIfNeededAsyncInternal(cancellationToken).OnSuccess(t => (T)t.Result); } } diff --git a/Storage/Storage/Public/AVFile.cs b/Storage/Storage/Public/AVFile.cs index 3d05878..b68073c 100644 --- a/Storage/Storage/Public/AVFile.cs +++ b/Storage/Storage/Public/AVFile.cs @@ -656,7 +656,7 @@ namespace LeanCloud { return Task.FromResult(0); } - string sessionToken = AVUser.CurrentUser.SessionToken; + string sessionToken = AVUser.CurrentUser?.SessionToken; return toAwait.OnSuccess(_ => { return FileController.DeleteAsync(state, sessionToken, cancellationToken); diff --git a/Storage/Storage/Public/AVObject.cs b/Storage/Storage/Public/AVObject.cs index d9f7479..a17e2de 100644 --- a/Storage/Storage/Public/AVObject.cs +++ b/Storage/Storage/Public/AVObject.cs @@ -709,7 +709,7 @@ string propertyName /// The cancellation token. public static Task SaveAllAsync( IEnumerable objects, CancellationToken cancellationToken) where T : AVObject { - return DeepSaveAsync(objects.ToList(), AVUser.CurrentUser.SessionToken, cancellationToken); + return DeepSaveAsync(objects.ToList(), AVUser.CurrentUser?.SessionToken, cancellationToken); } #endregion @@ -857,7 +857,7 @@ string propertyName return Task.FromResult(0); } - string sessionToken = AVUser.CurrentUser.SessionToken; + string sessionToken = AVUser.CurrentUser?.SessionToken; return toAwait.OnSuccess(_ => { return ObjectController.DeleteAsync(State, sessionToken, cancellationToken); @@ -902,7 +902,7 @@ string propertyName var states = uniqueObjects.Select(t => t.state).ToList(); return toAwait.OnSuccess(_ => { var deleteTasks = ObjectController.DeleteAllAsync(states, - AVUser.CurrentUser.SessionToken, + AVUser.CurrentUser?.SessionToken, cancellationToken); return Task.WhenAll(deleteTasks);