我正在处理项目,我希望迭代List并在返回结果之前将它们合并。基本上我正在尝试将private void btnStart_Click(object sender, RoutedEventArgs e)
{
string path = "";
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Properties | *.properties";
if (ofd.ShowDialog() == true)
{
path = ofd.FileName;
}
List<ServerProperties> serverProperties = new List<ServerProperties>();
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (!line.StartsWith("#"))
{
string[] lines = line.Split('=');
string property = lines[0];
string value = lines[1];
serverProperties.Add(new ServerProperties { Property = property, Value = value });
Debug.Print($"Property: {property} Value: {value}");
}
}
}
dgItems.ItemsSource = serverProperties;
}
对象转换为import java.util.*;
public class xy {
public static void main(String[] args) {
System.out.println("Enter a number with the format 'xy'. x is the row number and y the column number.");
Scanner scanner = new Scanner(System.in);
String[] xy = scanner.nextLine().split(charAt(1)); // not sure what to put here so I tried charAt but it doesn't work
String x = xy [0];
String y = xy [1];
}
}
。
一些相关代码。
Post.java
PostResponse
PostResponse.java
Post
获取帖子
public class Post {
@NonNull private User user;
@NonNull private String title;
@NonNull private String body;
@NonNull private Integer id;
}
上面提到的变换函数。
public final class PostResponse{
private Integer userId;
private Integer id;
private String title;
private String body;
}
但我没有收到订阅 @Override public Observable<List<Post>> posts() {
return postDataStore.getAllPosts()
.flatMap(postResponses -> Observable.fromIterable(postResponses)
.flatMap(postResponse -> postDataMapper.transform(postResponse))
// Receiving output here
.doOnNext(post -> Log.i(TAG, "posts: " + post))
.toList()
.toObservable()
// Not Receiving output here
.doOnNext(posts -> Log.i(TAG, "posts: " + posts)));
}
的{{1}}的任何数据。不确定是什么问题。
如果我犯了一个非常愚蠢的错误,我道歉。
答案 0 :(得分:1)
快速解决方法是添加:
emitter.onComplete();
在emitter.onNext(post);
但我建议重新考虑设计和逻辑。特别是您的转换不需要创建新的Observable
。我会用这样的方法重新设计你的tranform
方法:
Observable<Post> transform(PostResponse postResponse) {
return userRepository.user(postResponse.getUserId())
.map(user -> parseResponse(parseResponse, user));
}
Post parseResponse(PostResponse response, User user) {
final Post post = new Post(postResponse.getId());
post.setTitle(postResponse.getTitle());
post.setBody(postResponse.getBody());
post.setUser(user);
return post;
}
通过这种方式,您可以避免创建新的Observable
,避免内部订阅,并且您有一个结果的控制点,即最终结果Observable
的订阅。 / p>