2016-06-30 26 views
9

căn cứ hỏa lực lưu trữ tuyên bố here trong tài liệu iOS của mình rằng nócăn cứ hỏa lực lưu trữ không tải lên được trong bất lợi Điều kiện Mạng iOS

thực hiện cập nhật và tải về bất kể chất lượng mạng. Tải lên và tải xuống thật mạnh mẽ, có nghĩa là chúng bắt đầu lại khi chúng dừng lại

vì vậy người ta mong đợi nó xử lý mất kết nối khi tải lên, nhưng dường như không.

Với mã Swift sau trong iOS, tôi có thể thực hiện tải lên tốt khi có kết nối, nhưng nếu thiết bị không có kết nối hoặc bị ngắt kết nối khỏi mạng, nó sẽ chuyển đến tình trạng thất bại.

let storage = FIRStorage.storage().referenceForURL("VALID_URL_REMOVED") 

let imagesRef = storage.child("images/test.jpg") 

let data = UIImageJPEGRepresentation(observationImage!, 0.7); 

let uploadTask = imagesRef.putData(data!, metadata: nil) 

uploadTask.observeStatus(.Progress) { snapshot in 
    // Upload reported progress 
    if let progress = snapshot.progress { 
     let percentComplete = 100.0 * Double(progress.completedUnitCount)/Double(progress.totalUnitCount) 
     print("percent \(percentComplete)") 
    } 
} 

uploadTask.observeStatus(.Success) { snapshot in 
    // Upload completed successfully 
    print("success") 
} 

uploadTask.observeStatus(.Failure) { snapshot in 
    print("error") 
    print(snapshot.error?.localizedDescription) 
} 

Đầu ra gỡ lỗi cho mã này như sau.

/* 
percent 0.0 
percent 0.0044084949781492 
2016-06-30 11:49:16.480 Removed[5020:] <FIRAnalytics/DEBUG> Network status has changed. Code, status: 1, Disconnected 
percent 0.0044084949781492 
error 
Optional("An unknown error occurred, please check the server response.") 
*/ 

Thời gian thực Cơ sở dữ liệu ngoại tuyến của Firebase cũng được thiết lập với mã sau, nhưng tôi không chắc chắn liệu điều này có liên quan hay không.

FIRDatabase.database().persistenceEnabled = true 

Tôi cũng đã thử bằng tay đặt thời hạn như đã đề cập trong câu trả lời cho câu hỏi this sử dụng những dòng này, không có thay đổi.

let config = FIRStorage() 
config.maxUploadRetryTime = 1000000 

Có cách nào để xử lý các ngắt kết nối này mà không cần thực hiện chức năng từ đầu? Tui bỏ lỡ điều gì vậy?

Trả lời

0

Bạn đang thiếu quan sát viên. Ngay bây giờ bạn chỉ quan sát các sự kiện .success và .failure. Hãy thử thêm người quan sát cho .resume, .pause, .progress để xử lý các sự kiện khác nhau.

// Listen for state changes, errors, and completion of the upload. 

uploadTask.observe(.resume) { snapshot in 
// Upload resumed, also fires when the upload starts 
} 

uploadTask.observe(.pause) { snapshot in 
// Upload paused 
} 

uploadTask.observe(.progress) { snapshot in 
// Upload reported progress 
let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount) 
/Double(snapshot.progress!.totalUnitCount) 

} 

uploadTask.observe(.failure) { snapshot in 
if let error = snapshot.error as? NSError { 
switch (FIRStorageErrorCode(rawValue: error.code)!) { 
case .objectNotFound: 
    // File doesn't exist 
    break 
case .unauthorized: 
    // User doesn't have permission to access file 
    break 
case .cancelled: 
    // User canceled the upload 
    break 

/* ... */ 

case .unknown: 
    // Unknown error occurred, inspect the server response 
    break 
default: 
    // A separate error occurred. This is a good place to retry the upload. 
    break 
    } 

} 

} 
+0

Tôi có lẽ là sự hiểu lầm, nhưng dường như tôi không thêm người quan sát để tạm dừng và tiếp tục giải quyết vấn đề tải lên không thành công. Mặt khác, nó có thể chỉ là người ta phải tạm dừng và tiếp tục thủ công khi trạng thái mạng thay đổi. – ThatGuyJames

Các vấn đề liên quan