-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathParseExtensions.cs
63 lines (57 loc) · 2.67 KB
/
ParseExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
namespace Parse;
/// <summary>
/// Provides convenience extension methods for working with collections
/// of ParseObjects so that you can easily save and fetch them in batches.
/// </summary>
/// <summary>
/// Provides convenience extension methods for working with collections
/// of ParseObjects so that you can easily save and fetch them in batches.
/// </summary>
public static class ParseExtensions
{
/// <summary>
/// Fetches this object with the data from the server.
/// </summary>
/// <param name="obj">The ParseObject to fetch.</param>
/// <param name="cancellationToken">The cancellation token (optional).</param>
public static async Task<T> FetchAsync<T>(this T obj, CancellationToken cancellationToken = default) where T : ParseObject
{
var result = await obj.FetchAsyncInternal(cancellationToken).ConfigureAwait(false);
return (T) result;
}
/// <summary>
/// Fetches all objects in the collection from the server.
/// </summary>
public static async Task<IEnumerable<T>> FetchAllAsync<T>(this IEnumerable<T> objects, CancellationToken cancellationToken = default) where T : ParseObject
{
if (objects == null || !objects.Any()) return objects;
var result = await Task.WhenAll(objects.Select(obj => obj.FetchAsyncInternal(cancellationToken))).ConfigureAwait(false);
return result.Cast<T>();
}
/// <summary>
/// If this ParseObject has not been fetched (i.e. <see cref="ParseObject.IsDataAvailable"/> returns
/// false), fetches this object with the data from the server.
/// </summary>
/// <param name="obj">The ParseObject to fetch.</param>
/// <param name="cancellationToken">The cancellation token (optional).</param>
public static async Task<T> FetchIfNeededAsync<T>(this T obj, CancellationToken cancellationToken = default) where T : ParseObject
{
var result = await obj.FetchIfNeededAsyncInternal(cancellationToken).ConfigureAwait(false);
return (T) result;
}
/// <summary>
/// Fetches all objects in the collection from the server only if their data is not available.
/// </summary>
public static async Task<IEnumerable<T>> FetchAllIfNeededAsync<T>(this IEnumerable<T> objects, CancellationToken cancellationToken = default) where T : ParseObject
{
if (objects == null || !objects.Any())
return objects;
var result = await Task.WhenAll(objects.Select(obj => obj.FetchIfNeededAsyncInternal(cancellationToken))).ConfigureAwait(false);
return result.Cast<T>();
}
}