Authored by Olivier Poitrey

Do not use dispatch_io_write as it generate random crashes

Either it's buggy under iOS or I didn't use it the right way...
@@ -117,7 +117,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week @@ -117,7 +117,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
117 if (toDisk) 117 if (toDisk)
118 { 118 {
119 dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil); 119 dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil);
120 -  
121 dispatch_async(queue, ^ 120 dispatch_async(queue, ^
122 { 121 {
123 NSData *data = imageData; 122 NSData *data = imageData;
@@ -136,32 +135,15 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week @@ -136,32 +135,15 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
136 135
137 if (data) 136 if (data)
138 { 137 {
139 - if (![[NSFileManager defaultManager] fileExistsAtPath:_diskCachePath])  
140 - {  
141 - [[NSFileManager defaultManager] createDirectoryAtPath:_diskCachePath  
142 - withIntermediateDirectories:YES  
143 - attributes:nil  
144 - error:NULL];  
145 - } 138 + // Can't use defaultManager another thread
  139 + NSFileManager *fileManager = NSFileManager.new;
146 140
147 - NSString *path = [self cachePathForKey:key];  
148 - dispatch_io_t ioChannel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, [path UTF8String], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, queue, nil);  
149 - dispatch_data_t dispatchData = dispatch_data_create(data.bytes, data.length, queue, ^{[data self];});  
150 -  
151 - dispatch_io_write(ioChannel, 0, dispatchData, queue, ^(bool done, dispatch_data_t dispatchedData, int error)  
152 - {  
153 - if (error != 0)  
154 - {  
155 - NSLog(@"SDWebImageCache: Error writing image from disk cache: errno=%d", error);  
156 - }  
157 - if(done) 141 + if (![fileManager fileExistsAtPath:_diskCachePath])
158 { 142 {
159 - dispatch_io_close(ioChannel, 0); 143 + [fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
160 } 144 }
161 - });  
162 145
163 - dispatch_release(dispatchData);  
164 - dispatch_release(ioChannel); 146 + [fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
165 } 147 }
166 }); 148 });
167 dispatch_release(queue); 149 dispatch_release(queue);