gpt4 book ai didi

ios - 火力地堡存储 : child() doesn't work with iOS App

转载 作者:行者123 更新时间:2023-11-28 11:05:40 25 4
gpt4 key购买 nike

尝试从我的 Firebase 存储下载图像时出现以下错误:

Error Domain=FIRStorageErrorDomain Code=-13010 "Object 2xxxxxxx8/profile_pic does not exist."

(我显然把 x 放在那里是为了掩盖私有(private)信息。)

我正在使用以下代码添加对我的 Firebase 存储 的路径引用:

let storage = FIRStorage.storage()
let storageRef = storage.referenceForURL("gs://project-4xxxxxxxxxxxxx.appspot.com")
let profilePicReference = storageRef.child(signedInUser.uid + "/profile_pic")

我知道上面的代码很好,因为一切 都正常工作:我可以看到在我的存储空间中添加了一个文件夹,并且图像已上传到该文件夹​​中- 全部直接来 self 的 iOS 应用程序。

当我从我的 Firebase 存储中手动删除所述文件夹时(我通过 Firebase 门户网站执行此操作),问题开始了 - 只是因为我想验证一切是否正常,所以我删除了该文件夹以重新开始 - 期望上面的代码会一旦我再次运行应用程序就重新创建它 - 从那以后我一遍又一遍地收到这个错误。

真的没有意义。

Firebase 存储有任何怪癖或问题吗?某种必须解决的缓存?

如有任何提示,我们将不胜感激!

最佳答案

Are there any quirks or issues with Firebase Storage? Some sort of caching that has to be addressed?

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)")
}

}

//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) 将 .Success 观察器附加到 uploadTask。

如 Firebase 文档中所述,在 Monitor Upload Progress section ,如果 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 完成后执行下载代码并将下载回调添加到队列中某处,然后下载回调将执行。因为 sleep(10) 没有解决问题,这意味着必须将上传回调添加到主线程的执行队列中,并且 sleep 停止了主线程和队列中的任何执行。

这让我相信上传和下载回调被添加到主线程上的异步队列(它不是同步队列,否则回调将在命令)。我认为主线程上的异步队列意味着当主线程上有死时间时,队列中的任务将执行,并且当特定任务中有死时间时,您还可以在各个任务之间快速切换,例如等待 HTTP 响应。例如,如果主线程上的异步队列中有两个任务,则只要其中任何一个出现死时间,就会在主线程、task1 和 task2 之间快速切换。

关于ios - 火力地堡存储 : child() doesn't work with iOS App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37849318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com