using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using LeanCloud.Storage.Internal;
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
{
///
/// 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
{
return AVObject.SaveAllAsync(objects);
}
///
/// Saves all of the AVObjects in the enumeration. Equivalent to
/// calling
/// .
///
/// The objects to save.
/// The cancellation token.
public static Task SaveAllAsync(
this IEnumerable objects, CancellationToken cancellationToken) where T : AVObject
{
return AVObject.SaveAllAsync(objects, cancellationToken);
}
///
/// Fetches all of the objects in the enumeration. Equivalent to
/// calling .
///
/// The objects to save.
public static Task> FetchAllAsync(this IEnumerable objects)
where T : AVObject
{
return AVObject.FetchAllAsync(objects);
}
///
/// Fetches all of the objects in the enumeration. Equivalent to
/// calling
/// .
///
/// The objects to fetch.
/// The cancellation token.
public static Task> FetchAllAsync(
this IEnumerable objects, CancellationToken cancellationToken)
where T : AVObject
{
return AVObject.FetchAllAsync(objects, cancellationToken);
}
///
/// Fetches all of the objects in the enumeration that don't already have
/// data. Equivalent to calling
/// .
///
/// The objects to fetch.
public static Task> FetchAllIfNeededAsync(
this IEnumerable objects)
where T : AVObject
{
return AVObject.FetchAllIfNeededAsync(objects);
}
///
/// Fetches all of the objects in the enumeration that don't already have
/// data. Equivalent to calling
/// .
///
/// The objects to fetch.
/// The cancellation token.
public static Task> FetchAllIfNeededAsync(
this IEnumerable objects, CancellationToken cancellationToken)
where T : AVObject
{
return AVObject.FetchAllIfNeededAsync(objects, cancellationToken);
}
///
/// Constructs a query that is the or of the given queries.
///
/// The type of AVObject being queried.
/// An initial query to 'or' with additional queries.
/// 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
{
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
{
var queryString = new Dictionary();
if (includeKeys != null)
{
var encode = string.Join(",", includeKeys.ToArray());
queryString.Add("include", encode);
}
return obj.FetchAsyncInternal(queryString, cancellationToken).OnSuccess(t => (T)t.Result);
}
///
/// If this AVObject has not been fetched (i.e. returns
/// false), fetches this object with the data from the server.
///
/// The AVObject to fetch.
public static Task FetchIfNeededAsync(this T obj) where T : AVObject
{
return obj.FetchIfNeededAsyncInternal(CancellationToken.None).OnSuccess(t => (T)t.Result);
}
///
/// If this AVObject has not been fetched (i.e. returns
/// false), fetches this object with the data from the server.
///
/// The AVObject to fetch.
/// The cancellation token.
public static Task FetchIfNeededAsync(this T obj, CancellationToken cancellationToken)
where T : AVObject
{
return obj.FetchIfNeededAsyncInternal(cancellationToken).OnSuccess(t => (T)t.Result);
}
}
}