【发布时间】:2016-12-01 09:41:01
【问题描述】:
我正在从 Parse :'( 跨后端迁移到 Firebase,并且在了解 Firebase 围绕资产的实施和引用它们时遇到了一些问题。
基本上我需要一个 JSON 结构来详细说明一些文章,这些文章中的每一篇都会有一个图像。目前手动添加 JSON 是为了让应用程序引用最新文章。用于此的图像手动上传到 Firebase 的存储方面,并将其文件名添加到文章的 JSON 中。
我的应用调用JSON,以便在表格视图中填充文章标题的标签和同一单元格中的图像。
我遇到的问题是检索存储资产的图像 URL,以便将图像加载到 tableview -
首先我从数据库中调用文章的数据
FIRDatabaseReference *ref = [[FIRDatabase database] reference];
[[ref child:@"articles"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
NSArray *articles = (NSArray *)snapshot.value;} withCancelBlock:^(NSError * _Nonnull error) {
NSLog(@"%@", error.localizedDescription);
}];
从返回的快照被转换为模型对象 - 其中文章标题设置为模型对象以及生成的图像 URL
Article *article = [Article new];
article.title = [articleObj valueForKey:@"title"];
if ([articleObj objectForKey:@"image"] != [NSNull null]) {
// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com"];
// Points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"articleImages"];
// Note that you can use variables to create child values
FIRStorageReference *imagePath = [imagesRef child:articleObj[@"image"]];
if (imagePath){
article.imageURL = [NSURL URLWithString:imagePath.fullPath];
// Fetch the download URL
[imagePath downloadURLWithCompletion:^(NSURL *URL, NSError *error){
if (error != nil) {
// Handle any errors
NSLog(@"**ERROR %@", error.description);
NSLog(@"**ERROR %@", URL.absoluteString);
NSLog(@"**ERROR %@",article.title);
} else {
article.imageURL = URL;
}
}];
}
我在使用这种方法时遇到的问题(除了感觉它应该是一个标准属性的东西的很多代码之外)是文章标题立即可用,但“downloadURL”块需要更长的时间,所以我要么需要观察什么时候完成,而不是在每篇文章完成获取其图片 URL 之前重新加载我的表格
所以问题: 是否有另一种非阻塞方式来获取图像 URL 或者我在存储门户中看到的 URL 引用是否足够可靠,可以在我的 JSON 中使用,而不是对图像名称的引用(因此将 URL 存储在我的数据库中,而不是图像名称)
也供参考,我正在使用 SDWebimage 延迟加载图像,但由于上述检索 URL 的延迟,第一次tablereloads 时 URL 目前不存在
提前致谢!
【问题讨论】:
标签: ios objective-c image firebase firebase-storage