Firebase 存储是否有任何怪癖或问题?某种必须解决的缓存?
一个UploadTask
执行asynchronously
。如果我在上传图像后立即尝试下载图像,我可以重现您的错误。发生的情况是下载代码在图像完成上传之前执行,从而产生图像不存在错误。通过在回调中打印一些消息,您可以看到下载代码执行得太早:
let storage = FIRStorage.storage()
let storageRef = storage.reference() //You don't need to explicitly write the url in your code.
//The config file GoogleService-Info.plist will handle that.
let imageRef = storageRef.child("images/align_menu.tiff")
let localURL = NSBundle.mainBundle().URLForResource(
"align_menu",
withExtension: "tiff"
)!
//Upload the image:
let uploadTask = imageRef.putFile(localURL, metadata: nil) { (metadata, error) -> Void in
if let returnedError = error {
// Uh-oh, an error occurred!
print("[My Upload Error]: \(returnedError)")
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
print("[My Upload Success]:")
let downloadURL = metadata!.downloadURL()!
print("[URL for download]: \(downloadURL)")
}
}
//Download the image:
imageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if let returnedError = error {
// Uh-oh, an error occurred!
print("[My Download Error]: \(returnedError)")
}
else {
print("[My Download Success]:")
if let validImage = UIImage(data: data!) {
NSOperationQueue.mainQueue().addOperationWithBlock() {
self.imageView.image = validImage
}
}
}
}
该代码产生输出:
[My Download Error]: ...."Object images/align_menu.tiff does not exist."...
然后几秒钟后我看到了输出:
[My Upload Success]:
[URL for download]: ...
这表明下载回调在上传回调之前执行。我不能完全弄清楚为什么会发生这种情况的细节——但显然回调没有添加到串行队列中。*
要解决异步问题,您有几种选择:
1)将下载代码放在上传代码的回调中。
这样,在图像成功上传之前,下载不会开始执行。在我这样做之后,在运行应用程序之前使用 Firebase 存储网页删除图像对我的上传/下载没有有害影响,并且消息按预期顺序输出:
[My Upload Success]:
[URL for download]: ...
[My Download Success]:
2)为uploadTask 附加一个.Success 观察者。
如 Firebase 文档中所述,在Monitor Upload Progress 部分中,您可以在 uploadTask 成功上传图片时收到通知:
let storage = FIRStorage.storage()
let storageRef = storage.reference() //You don't need to explicitly write the url in your code.
//The config file GoogleService-Info.plist will handle that.
let imageRef = storageRef.child("images/align_menu.tiff")
let localURL = NSBundle.mainBundle().URLForResource(
"align_menu",
withExtension: "tiff"
)!
//Upload the image:
let uploadTask = imageRef.putFile(localURL, metadata: nil) { (metadata, error) -> Void in
if let returnedError = error {
// Uh-oh, an error occurred!
print("[My Upload Error]: \(returnedError)")
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
print("[My Upload Success]:")
let downloadURL = metadata!.downloadURL()!
print("[URL for download]: \(downloadURL)")
}
}
let observer = uploadTask.observeStatus(.Success) { (snapshot) -> Void in
//Download the image:
imageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if let returnedError = error {
// Uh-oh, an error occurred!
print("[My Download Error]: \(returnedError)")
}
else {
print("[My Download Success]:")
if let validImage = UIImage(data: data!) {
NSOperationQueue.mainQueue().addOperationWithBlock() {
self.imageView.image = validImage
}
}
}
}
}
3)上传成功时使用 Grand Central Dispatch 通知您。
您无法控制回调添加到哪些队列(Firebase 方法实现决定),但您可以使用 Grand Central Dispatch 在任意代码完成执行时通知您。以下对我有用:
let storage = FIRStorage.storage()
let storageRef = storage.reference() //You don't need to explicitly write the url in your code.
//The config file GoogleService-Info.plist will handle that.
let imageRef = storageRef.child("images/align_menu.tiff")
let localURL = NSBundle.mainBundle().URLForResource(
"align_menu",
withExtension: "tiff"
)!
let myExecutionGroup = dispatch_group_create()
dispatch_group_enter(myExecutionGroup)
//Upload the image:
let _ = imageRef.putFile(localURL, metadata: nil) { (metadata, error) -> Void in
if let returnedError = error {
// Uh-oh, an error occurred!
print("[My Upload Error]: \(returnedError)")
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
print("[My Upload Success]:")
let downloadURL = metadata!.downloadURL()!
print("[URL for download]: \(downloadURL)")
dispatch_group_leave(myExecutionGroup)
}
}
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
dispatch_group_notify(myExecutionGroup, queue) {
//This callback executes for every dispatch_group_leave().
//Download the image:
imageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
if let returnedError = error {
// Uh-oh, an error occurred!
print("[My Download Error]: \(returnedError)")
}
else {
print("[My Download Success]:")
if let validImage = UIImage(data: data!) {
NSOperationQueue.mainQueue().addOperationWithBlock() {
self.imageView.image = validImage
}
}
}
}
}
*
我尝试在原始上传代码和下载代码之间放置一个sleep(10)
,但这并没有缓解问题。我认为如果上传回调在后台线程上执行,那么上传回调将有时间在主线程睡眠时完成,然后在睡眠完成后下载代码将执行并将下载回调添加到队列中某处,然后下载回调将执行。因为 sleep(10) 没有解决问题,这意味着上传回调必须被添加到主线程的执行队列中,并且 sleep 停止了主线程和队列中的任何内容的执行。
这使我相信上传和下载回调被添加到主线程上的异步队列中(它不是同步队列,否则回调将按顺序执行)。我认为主线程上的异步队列意味着当主线程上存在死时间时,队列中的任务将执行,并且当特定任务中存在死时间时,您还可以在各种任务之间快速切换,例如等待 HTTP 响应。例如,如果主线程上的异步队列中有两个任务,那么只要其中任何一个有死时间,就会在主线程、task1 和 task2 之间快速切换。