将“[Publishers.Map<URLSession.DataTaskPublisher, [ProductRep]?>]”转换为 [ProductRep]
Convert '[Publishers.Map<URLSession.DataTaskPublisher, [ProductRep]?>]' to [ProductRep]
如何找到 flatMap、reduce 等将其转换为 ProductRep 数组?
我目前得到 Cannot convert value of type 'Publishers.Map<URLSession.DataTaskPublisher, [StoreService.ProductRep]?>' to closure result type 'StoreService.ProductRep'
- 因为我真的不明白如何将 Publishers.Map
变成实际的东西。
这旨在成为使用 map/flatMap.
的更大的发布者和订阅者网络的一部分
let mapResult: [ProductRep] = parsePageURIs(firstPage, jsonDict)
.map { pageUri in self.importProductsPublisher(pageUri, urlSession: urlSession, firstPage: false) }
.map { publisher in publisher.map {(productsData, productsResponse) in
return self.handleImportProductsResponse(data: productsData, response: productsResponse, category: category, urlSession: urlSession)
}
}
func importProductsPublisher(_ uri: String, urlSession: URLSession, firstPage: Bool) -> URLSession.DataTaskPublisher { /* Start the network query */ }
func handleImportProductsResponse(data: Data, response: URLResponse, category: CategoryRep, urlSession: URLSession) -> [ProductRep]? { /* Handle the result of the network query and parse into an array of items (ProductRep) */ }
我认为这里发生了两件事。首先,您缺少某种接收器,通常您会看到一系列发布者以 .sink 或 .assign
结尾
whatever.map{ ... }.sink{ items in
//Do something with items in this closure
}
其次,您似乎是将页面 URI 映射到一组发布者,而不是单个发布者。然后你在地图中有一个嵌套地图,它还没有解决任何问题。
您可以使用合并发布者之一,例如合并多个和收集以从一个发布者减少到多个:
https://developer.apple.com/documentation/combine/publishers/mergemany/3209693-collect
一个基本的集合示例:
let arrayOfPublishers = (0...10).map{ int in
return Just(int)
}
Publishers.MergeMany(arrayOfPublishers).collect().sink { allTheInts in
//allTheInts is of type [Int]
print(allTheInts)
}
我认为你需要这样的东西:
let productPublishers = parsePageURIs(firstPage, jsonDict).map { pageUri in
return self.importProductsPublisher(pageUri, urlSession: urlSession, firstPage: false).map {(productsData, productsResponse) in
return self.handleImportProductsResponse(data: productsData, response: productsResponse, category: category, urlSession: urlSession)
}
}
Publishers.MergeMany(productPublishers).collect().sink { products in
//Do somethings with the array of products
}
在映射数据请求发布者后,将 uris 映射到模型中的发布者结果,然后使用 MergeMany & collect 合并所有发布者以将各个结果放入一个数组中,最后是实际触发一切发生的接收器。
如何找到 flatMap、reduce 等将其转换为 ProductRep 数组?
我目前得到 Cannot convert value of type 'Publishers.Map<URLSession.DataTaskPublisher, [StoreService.ProductRep]?>' to closure result type 'StoreService.ProductRep'
- 因为我真的不明白如何将 Publishers.Map
变成实际的东西。
这旨在成为使用 map/flatMap.
let mapResult: [ProductRep] = parsePageURIs(firstPage, jsonDict)
.map { pageUri in self.importProductsPublisher(pageUri, urlSession: urlSession, firstPage: false) }
.map { publisher in publisher.map {(productsData, productsResponse) in
return self.handleImportProductsResponse(data: productsData, response: productsResponse, category: category, urlSession: urlSession)
}
}
func importProductsPublisher(_ uri: String, urlSession: URLSession, firstPage: Bool) -> URLSession.DataTaskPublisher { /* Start the network query */ }
func handleImportProductsResponse(data: Data, response: URLResponse, category: CategoryRep, urlSession: URLSession) -> [ProductRep]? { /* Handle the result of the network query and parse into an array of items (ProductRep) */ }
我认为这里发生了两件事。首先,您缺少某种接收器,通常您会看到一系列发布者以 .sink 或 .assign
结尾whatever.map{ ... }.sink{ items in
//Do something with items in this closure
}
其次,您似乎是将页面 URI 映射到一组发布者,而不是单个发布者。然后你在地图中有一个嵌套地图,它还没有解决任何问题。
您可以使用合并发布者之一,例如合并多个和收集以从一个发布者减少到多个: https://developer.apple.com/documentation/combine/publishers/mergemany/3209693-collect
一个基本的集合示例:
let arrayOfPublishers = (0...10).map{ int in
return Just(int)
}
Publishers.MergeMany(arrayOfPublishers).collect().sink { allTheInts in
//allTheInts is of type [Int]
print(allTheInts)
}
我认为你需要这样的东西:
let productPublishers = parsePageURIs(firstPage, jsonDict).map { pageUri in
return self.importProductsPublisher(pageUri, urlSession: urlSession, firstPage: false).map {(productsData, productsResponse) in
return self.handleImportProductsResponse(data: productsData, response: productsResponse, category: category, urlSession: urlSession)
}
}
Publishers.MergeMany(productPublishers).collect().sink { products in
//Do somethings with the array of products
}
在映射数据请求发布者后,将 uris 映射到模型中的发布者结果,然后使用 MergeMany & collect 合并所有发布者以将各个结果放入一个数组中,最后是实际触发一切发生的接收器。