【问题标题】:Total upload count is 0 when uploading file to Firebase using iOS SDK使用 iOS SDK 将文件上传到 Firebase 时,上传总数为 0
【发布时间】:2016-10-02 17:30:01
【问题描述】:

我想在将 iOS 应用中录制的视频文件上传到 Firebase 时向用户显示进度条,因此我使用了 SDK 的 observeStatus 功能:

// Create a root reference
FIRStorageReference *storageRef = [_storage reference];

// Create a reference to the video
FIRStorageReference *videoRef = [storageRef child:uuidString];

// Upload the file
FIRStorageUploadTask* uploadTask = [videoRef putFile:url metadata:nil];

[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
    // A progress event occurred
    double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);

    NSLog(@"Video with ID %d recording upload progress: %f, completed unit count %lld, total unit count %lld", video.videoId, percentComplete, snapshot.progress.completedUnitCount, snapshot.progress.totalUnitCount);
}];

但是,“总单位数”似乎总是为 0,这意味着计算出的完成百分比在整个除以 0 的情况下有点疯狂:

Video with ID 3 recording upload progress: inf, completed unit count 163922, total unit count 0

我做错了吗?按照here 所述的文档进行操作。

[也在 Swift 中复制]

【问题讨论】:

  • 我看到了同样的问题,还没有运气。

标签: ios objective-c swift firebase firebase-storage


【解决方案1】:

我们实际上正确地创建了NSProgress,并为内存中的上传(putData)进行了适当的更新,但没有获得文件上传的文件大小(putFile),因为显然我没有发现文件属性/它们并不总是正确的。

我正在尽快修复,现在您可以使用NSFileManager.defaultManager().attributesOfItemAtPath(url.path!)[NSFileSize]!.longLongValue 解决方法来获取文件大小:)

编辑(2016 年 10 月 10 日):这个问题已经解决了,如果你使用最新版本的 Firebase 存储,你不会有这个问题 :)

【讨论】:

  • 它对我不起作用。它最多提供 %25。 completedUnit 和 fileSizes 似乎不是相同的“单位”。想法?
  • 现在看来已经修复了。
  • 是的,这是几个月前修复的——很抱歉没有更新。
【解决方案2】:

这不是我们想要的答案,但我猜 Firebase SDK 中存在一个错误,它没有正确创建 NSProgress 实例。

因此,作为一种变通方法,您可以在创建上传任务之前捕获文件大小。我正在使用这个扩展(为了方便起见,这对 IUO 来说超级崩溃)。

extension NSFileManager {
    func fileSize(atUrl url: NSURL) -> Int64 {
        return try! NSFileManager.defaultManager().attributesOfItemAtPath(url.path!)[NSFileSize]!.longLongValue
    }
}

捕获文件大小值并在计算中使用它代替totalUnitCount

FWIW 我在https://firebase.google.com/support/contact/bugs-features/ 提交了一个关于这个问题的错误

【讨论】:

    【解决方案3】:

    已编辑

    使用data 上传工作正常:

        NSData *data = // ... some real data to upload
    
        FIRStorageReference *storageRef = [storage referenceForURL:@"gs://<YOURPATH>"];
    
        // Upload the file to the path
        FIRStorageUploadTask *uploadTask = [storageRef putData:data metadata:nil completion:^(FIRStorageMetadata * _Nullable metadata, NSError * _Nullable error) {
    
            if (error != nil) {
                // Error handle it
            } else {
                NSString *urlToDownload = metadata.downloadURL;
            }
    
        }];
    
        // Add a progress observer to an upload task
        [uploadTask observeStatus:FIRStorageTaskStatusProgress
                              handler:^(FIRStorageTaskSnapshot *snapshot) {
                                  // A progress event occurred
                                  double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
                                  NSLog(@"\n\nloading...%f",percentComplete);
                              }];
    

    我希望这可以帮助 OP 或其他任何人。

    【讨论】:

    • 我当然可以试一试,但认为它可能无法解决问题,因为在日志跟踪中上传期间,“完成的单元数”数字会按预期增加。
    • 是的,不幸的是,这是正确的行为:数据有一个长度属性,我们可以从中获取“文件大小”,而 URL 没有类似的机制(你必须深入了解一些层:stackoverflow.com/questions/5743856/finding-files-size)。很快就会解决这个问题。
    猜你喜欢
    • 2019-08-02
    • 2021-10-01
    • 2019-03-29
    • 1970-01-01
    • 2021-03-19
    • 2018-11-24
    • 2020-11-21
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多