Skip to content

Commit 66b2614

Browse files
committed
Fix issues related StorageUI and SDWebImage

1. The FIRStorage throw exception on passed url contains object path 2. The progress block may been called on main queue, should use another coder queue for decoding 3. The expectedSize should use URLResponse's info to check, but not the bodyLength
1 parent e7a80d9 commit 66b2614

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

Storage/FirebaseStorageUI/FUIStorageImageLoader.m

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ @interface FIRStorageTask ()
3131

3232
@end
3333

34+
@interface FUIStorageImageLoader ()
35+
36+
@property (nonatomic, strong) dispatch_queue_t coderQueue;
37+
38+
@end
39+
3440
@implementation FUIStorageImageLoader
3541

3642
+ (FUIStorageImageLoader *)sharedLoader {
@@ -45,7 +51,8 @@ + (FUIStorageImageLoader *)sharedLoader {
4551
- (instancetype)init {
4652
self = [super init];
4753
if (self) {
48-
self.defaultMaxImageSize = 10e6;
54+
_defaultMaxImageSize = 10e6;
55+
_coderQueue = dispatch_queue_create("com.google.firebaseui.storage.coderQueue", DISPATCH_QUEUE_SERIAL);
4956
}
5057
return self;
5158
}
@@ -66,8 +73,9 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
6673
FIRStorageReference *storageRef = url.sd_storageReference;
6774
if (!storageRef) {
6875
// Create Storage Reference from URL
69-
FIRStorage *storage = [FIRStorage storageWithURL:url.absoluteString];
70-
storageRef = storage.reference;
76+
NSString *bucketUrl = [NSString stringWithFormat:@"gs://%@", url.host];
77+
FIRStorage *storage = [FIRStorage storageWithURL:bucketUrl];
78+
storageRef = [storage referenceWithPath:url.path];
7179
url.sd_storageReference = storageRef;
7280
}
7381

@@ -97,13 +105,15 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
97105
return;
98106
}
99107
// Decode the image with data
100-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
101-
UIImage *image = SDImageLoaderDecodeImageData(data, url, options, context);
102-
dispatch_main_async_safe(^{
103-
if (completedBlock) {
104-
completedBlock(image, data, nil, YES);
105-
}
106-
});
108+
dispatch_async(self.coderQueue, ^{
109+
@autoreleasepool {
110+
UIImage *image = SDImageLoaderDecodeImageData(data, url, options, context);
111+
dispatch_main_async_safe(^{
112+
if (completedBlock) {
113+
completedBlock(image, data, nil, YES);
114+
}
115+
});
116+
}
107117
});
108118
}];
109119
// Observe the progress changes
@@ -116,14 +126,24 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
116126
GTMSessionFetcher *fetcher = task.fetcher;
117127
// Get the partial image data
118128
NSData *partialData = [fetcher.downloadedData copy];
119-
// Get the finish status
120-
BOOL finished = (fetcher.downloadedLength >= fetcher.bodyLength);
121-
// This progress block is callbacked on global queue, so it's OK to decode
122-
UIImage *image = SDImageLoaderDecodeProgressiveImageData(partialData, url, finished, task, options, context);
123-
if (image) {
124-
dispatch_main_async_safe(^{
125-
if (completedBlock) {
126-
completedBlock(image, partialData, nil, NO);
129+
// Get response
130+
int64_t expectedSize = fetcher.response.expectedContentLength;
131+
expectedSize = expectedSize > 0 ? expectedSize : 0;
132+
int64_t receivedSize = fetcher.downloadedLength;
133+
if (expectedSize != 0) {
134+
// Get the finish status
135+
BOOL finished = receivedSize >= expectedSize;
136+
// This progress block may be called on main queue or global queue (depends configuration), always dispatched on coder queue
137+
dispatch_async(self.coderQueue, ^{
138+
@autoreleasepool {
139+
UIImage *image = SDImageLoaderDecodeProgressiveImageData(partialData, url, finished, task, options, context);
140+
if (image) {
141+
dispatch_main_async_safe(^{
142+
if (completedBlock) {
143+
completedBlock(image, partialData, nil, NO);
144+
}
145+
});
146+
}
127147
}
128148
});
129149
}

0 commit comments

Comments
 (0)