* ObjectTest.cs:
* LCObject.cs: chore: 支持 FetchAll
parent
7bd2cd21d3
commit
9d3d6092f0
|
@ -166,5 +166,19 @@ namespace Storage.Test {
|
|||
Assert.AreEqual(intList[1], 2);
|
||||
Assert.AreEqual(intList[2], 3);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FetchAll() {
|
||||
List<LCObject> list = new List<LCObject> {
|
||||
LCObject.CreateWithoutData("Hello", "5e8fe86938ed12000870ae82"),
|
||||
LCObject.CreateWithoutData("Hello", "5e8fe867158a7a0006be0feb"),
|
||||
LCObject.CreateWithoutData("Hello", "5e8fe84e5c385800081a1d64"),
|
||||
};
|
||||
await LCObject.FetchAll(list);
|
||||
Assert.Greater(list.Count, 0);
|
||||
foreach (LCObject obj in list) {
|
||||
Assert.NotNull(obj["intList"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.ObjectModel;
|
||||
using LeanCloud.Storage.Internal.Object;
|
||||
using LeanCloud.Storage.Internal.Operation;
|
||||
using LeanCloud.Storage.Internal.Codec;
|
||||
|
@ -412,6 +413,43 @@ namespace LeanCloud.Storage {
|
|||
return this;
|
||||
}
|
||||
|
||||
public static async Task<IEnumerable<LCObject>> FetchAll(IEnumerable<LCObject> objects) {
|
||||
if (objects == null || objects.Count() == 0) {
|
||||
throw new ArgumentNullException(nameof(objects));
|
||||
}
|
||||
|
||||
IEnumerable<LCObject> uniqueObjects = objects.Where(item => item.ObjectId != null);
|
||||
List<Dictionary<string, object>> requestList = uniqueObjects.Select(item => {
|
||||
string path = $"/{LCApplication.APIVersion}/classes/{item.ClassName}/{item.ObjectId}";
|
||||
return new Dictionary<string, object> {
|
||||
{ "path", path },
|
||||
{ "method", "GET" }
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
Dictionary<string, object> data = new Dictionary<string, object> {
|
||||
{ "requests", LCEncoder.Encode(requestList) }
|
||||
};
|
||||
List<Dictionary<string, object>> results = await LCApplication.HttpClient.Post<List<Dictionary<string, object>>>("batch",
|
||||
data: data);
|
||||
Dictionary<string, LCObjectData> dict = new Dictionary<string, LCObjectData>();
|
||||
foreach (Dictionary<string, object> item in results) {
|
||||
if (item.TryGetValue("error", out object error)) {
|
||||
int code = (int)error;
|
||||
string message = item["error"] as string;
|
||||
throw new LCException(code, message);
|
||||
}
|
||||
Dictionary<string, object> d = item["success"] as Dictionary<string, object>;
|
||||
string objectId = d["objectId"] as string;
|
||||
dict[objectId] = LCObjectData.Decode(d);
|
||||
}
|
||||
foreach (LCObject obj in objects) {
|
||||
LCObjectData objData = dict[obj.ObjectId];
|
||||
obj.Merge(objData);
|
||||
}
|
||||
return objects;
|
||||
}
|
||||
|
||||
public static void RegisterSubclass<T>(string className, Func<T> constructor) where T : LCObject {
|
||||
Type classType = typeof(T);
|
||||
LCSubclassInfo subclassInfo = new LCSubclassInfo(className, classType, constructor);
|
||||
|
|
Loading…
Reference in New Issue