You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 14下AFNetworking频繁发起请求后出现「No space left on device」错误排查求助

iOS 14 AFNetworking "No space left on device" Error with Multiple Login Attempts

Hey there, I’ve seen other developers run into this exact issue on iOS 14—let’s break down what’s probably happening and share actionable fixes you can test out:

What’s Causing This?

The Error Domain=NSPOSIXErrorDomain Code=28 "No space left on device" error is tricky because it’s tied to disk storage, not your app’s available RAM. Here’s why iOS 14 triggers this when older versions didn’t:

  • Tighter Disk Cache Limits: iOS 14 reduced the default disk cache quota for URLSession (which AFNetworking uses under the hood). Your 158 requests per login generate a ton of cached data, and after 2-3 logins, the cache hits the new, stricter limit.
  • Uncleaned Temporary Files: AFNetworking creates temp files for request/response handling. iOS 14’s automatic temp file cleanup doesn’t keep up with the volume of requests you’re sending, leading to a buildup that eats up disk space.
  • Outdated AFNetworking Defaults: The cache and session configurations in AFNetworking (built for older iOS versions) don’t account for iOS 14’s stricter storage policies.

Fixes to Try

1. Disable Unnecessary Caching for Login Requests

Most login-stage requests (auth tokens, user profile data) don’t need to be cached. Override the cache policy for these requests to avoid writing redundant data to disk:

// Objective-C
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:yourLoginAPIURL];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
// Swift
var request = URLRequest(url: yourLoginAPIURL)
request.cachePolicy = .reloadIgnoringLocalCacheData

You can also set this as the default policy for your AFNetworking SessionManager if most login traffic doesn’t benefit from caching.

2. Customize URLCache Limits

AFNetworking uses the shared URLCache by default. Shrink its disk capacity to stay under iOS 14’s limits:

// Objective-C
URLCache *customCache = [[URLCache alloc] initWithMemoryCapacity:20*1024*1024 // 20MB in RAM
                                                   diskCapacity:50*1024*1024 // 50MB on disk
                                                       diskPath:nil];
[URLCache setSharedURLCache:customCache];
// Swift
let customCache = URLCache(memoryCapacity: 20 * 1024 * 1024,
                           diskCapacity: 50 * 1024 * 1024,
                           diskPath: nil)
URLCache.shared = customCache

This prevents your app’s cache from ballooning to a size that triggers the "no space" error.

3. Manually Clean Temporary Files

AFNetworking stores temp files in the system’s temp directory. Add cleanup logic after each login flow completes to prevent buildup:

// Objective-C
NSString *tempDir = NSTemporaryDirectory();
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *tempFiles = [fileManager contentsOfDirectoryAtPath:tempDir error:&error];
if (!error) {
    for (NSString *file in tempFiles) {
        [fileManager removeItemAtPath:[tempDir stringByAppendingPathComponent:file] error:nil];
    }
}
// Swift
if let tempDir = NSTemporaryDirectory() as String?,
   let tempFiles = try? FileManager.default.contentsOfDirectory(atPath: tempDir) {
    for file in tempFiles {
        try? FileManager.default.removeItem(atPath: tempDir + file)
    }
}

4. Reduce Request Volume

158 requests per login is a lot—look for ways to batch calls. For example, combine multiple GET requests for user settings into a single batch API endpoint. Fewer requests mean less cache and temp file overhead, which directly lowers the chance of hitting storage limits.

5. Verify Disk Usage with Xcode

Use Xcode’s Devices and Simulators window (Window > Devices and Simulators) to check your app’s disk breakdown:

  • Select your device and app
  • Click "Download Container" to inspect the app’s file system
  • Check the Library/Caches and tmp directories to confirm if cache/temp files are the culprit.

Community Context

Several developers have reported this exact issue on Apple’s Developer Forums and Stack Overflow—iOS 14’s stricter storage enforcement for URLSession is the common thread. Adjusting your AFNetworking cache and temp file handling should fix the problem without major changes to your core network logic.

内容的提问来源于stack exchange,提问作者Oliver Schobel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:47:42