这是一个可观察的序列,用于从Web服务检索分页数据。每个Web服务响应都包含一个nextRecordsUrl
,指示从哪里获取下一组记录。将此Observable转换为更可重用的东西的最佳方法是什么?
网络服务设置:
var auth = new AuthenticationClient ();
await auth.UsernamePasswordAsync (consumerKey, consumerSecret, userName, password + passwordSecurityToken);
var forceClient = new ForceClient (auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
The Observable:
var observable = Observable.Create<QueryResult<Account>> (async (IObserver<QueryResult<Account>> o) =>
{
try
{
var queryResult = await forceClient.QueryAsync<Account> ("SELECT Id, Name from Account");
if (queryResult != null)
{
o.OnNext (queryResult);
while (!string.IsNullOrEmpty (queryResult.nextRecordsUrl))
{
queryResult = await forceClient.QueryContinuationAsync<Account> (queryResult.nextRecordsUrl);
if (queryResult != null)
{
o.OnNext (queryResult);
}
}
}
o.OnCompleted ();
}
catch (Exception ex)
{
o.OnError (ex);
}
return () => {};
});
订阅观察结果并收集结果:
var accounts = new List<Account> ();
observable.Subscribe (
observer => accounts.AddRange (observer.records),
ex => Console.WriteLine (ex.Message),
() => {});
编辑:使用Brandon的解决方案我现在可以使用Aggregate生成结果列表
List<Account> accounts = await forceClient.QueryPages<Account> ("SELECT Id, Name from Account")
.Aggregate (new List<Account> (), (list, value) =>
{
list.AddRange (value.records);
return list;
});
答案 0 :(得分:2)
这是你想要的东西吗?
public static IObservable<QueryResult<TResult>> QueryPages<TResult>(this ForceClient forceClient, string query)
{
return Observable.Create<QueryResult<T>> (async (observer, token) =>
{
// No need for try/catch. Create() will call OnError if your task fails.
// Also no need for OnCompleted(). Create() calls it when your task completes
var queryResult = await forceClient.QueryAsync<TResult> (query);
while (queryResult != null)
{
observer.OnNext (queryResult);
// check the token *after* we call OnNext
// because if an observer unsubscribes
// it typically occurs during the notification
// e.g. they are using .Take(..) or
// something.
if (string.IsNullOrEmpty(queryResult.nextRecordsUrl) ||
token.IsCancellationRequested)
{
break;
}
queryResult = await forceClient.QueryContinuationAsync<TResult> (queryResult.nextRecordsUrl);
}
// No need to return anything. Just the Task itself is all that Create() wants.
}
});
// Usage:
var forceClient = // ...
var foos = forceClient.QueryPages<Foo>("SELECT A, B, C FROM Foo");
注意我将其切换到提供取消令牌的重载,以便在观察者取消订阅时您可以停止抓取页面(即使观察者已经停止侦听,您的原始版本也会继续抓取页面)。另请注意,异步Create
等待您的Task
并为您拨打OnError
或OnCompleted
,因此您无需担心大部分时间。
答案 1 :(得分:2)
信不信由你,Rx-Expiremental库(也由MS维护)有一个名为Expand
的运算符。 Expand用于从一个observable中获取每个元素,并通过一个函数运行它,该函数生成另一个相同类型的observable。那个可观察的东西然后被压平成原始的,并且每个项目都经历了相同的过程。
想象一下,给定一个带有可观察子节点的树节点。您可以使用expand来轻松遍历此树。由于链表只是树的约束版本,并且由于您拥有的实际上是每个节点都是可观察的链表,因此您可以使用expand。
public static IObservable<QueryResult<TResult>> QueryPages<TResult>(this ForceClient forceClient, string query)
{
return Observable.FromAsync(() => forceClient.QueryAsync<TResult>(query))
.Where(QueryResultIsValid)
.Expand(result =>
Observable.FromAsync(() => forceClient.QueryContinuationAsync<TResult>(queryResult.nextRecordsUrl))
.Where(QueryResultIsValid)
);
}
public static bool QueryResultIsValid(QueryResult<TResult> result)
{
return result != null;
}