我在代码中看到一个非常奇怪且随机的问题,我无法追查。从AFNetworking JSON请求方法返回时,我的数据模型init方法崩溃了。当应用程序崩溃时,我可以退回调用堆栈以调试JSON请求/响应的内容。奇怪的部分是我检查URL,请求和resonseJSON。 responseJSON与URL /请求的预期结果不匹配。这就像我得到一些其他API方法调用和数据。因为数据/ JSON不是我预期的应用程序将在模型init上崩溃。
我得到的数据通常是不同的,并不总是相同。有时数据来自端点A,有时来自B,它永远不会一致。然而,它确实在同一个模型对象中始终崩溃。
请求端点数据,但我返回端点B数据。当我在崩溃时调试AFHttpOperation时,我发现这是结果。这几乎就像是两个电话越过并且是某种竞争条件。下面是我的模型对象,Rest客户端和模型访问层的示例。
模型对象
@implementation Applications
- (id)initWithData:(NSDictionary *)appData forLocation:(Location *)location inCategory:(Category *)category {
// appData is the JSON returned by The Rest Client and AFNetworking
self = [super init];
DDLogVerbose(@"appData = %@", appData);
if (self) {
_location = location;
_listeners = [NSMutableArray mutableArrayUsingWeakReferences];
_devices = [[NSMutableDictionary alloc] init];
_category = category;
_subscriptions = [Utility sanitizeArray:appData[@"Subscriptions"]];
}
return self;
}
@end
@implementation Location
- (void)refreshApplications {
[[Model shared] appsForLocation:self
success:^(NSObject *obj) {
self.apps = nil; //we have to get our apps again
self.apps = [NSMutableArray array];
NSArray *newApps = (NSArray *) obj;
for (NSDictionary *app in newApps) {
**// This is where it's crashing!**
Applications *newApp = [[Applications alloc] initWithData:app
forLocation:self
inCategory:[[SmartAppCategory alloc] init]];
[self.apps addObject:newApp];
}
[self notifyListeners];
}
error:nil];
}
@end
休息客户
@interface Rest
+ (Rest *)sharedClient;
- (void)GET:(NSString *)path parameters:(NSDictionary *)params success:(SuccessCallback)sCallback error:(ErrorCallback)eCallback;
@end
@implementation Rest
+ (Rest *)sharedClient {
static dispatch_once_t token;
static Rest *shared = nil;
dispatch_once(&token, ^{
shared = [[Rest alloc] init];
});
return shared;
}
- (id)init {
self = [super init];
if (self) {
[self createClients];
}
return self;
}
- (void)createClients {
// Setup the Secure Client
// Private implementation properties
self.secureClient = [[AFOAuth2Client alloc] initWithBaseURL:baseUrl clientID:OAUTH2_CLIENT_ID secret:OAUTH2_CLIENT_SECRET];
[self.secureClient setParameterEncoding:AFJSONParameterEncoding];
AFOAuthCredential *credential = (AFOAuthCredential *) [NSKeyedUnarchiver unarchiveObjectWithData:[KeyChainStore dataForKey:KEYCHAIN_SETTINGS_AFOAuthCredential]];
if (credential) {
[self.secureClient setAuthorizationHeaderWithToken:credential.accessToken];
}
// Setup the Anonymous Client
self.anonymousClient = [[AFHTTPClient alloc] initWithBaseURL:baseUrl];
[self.anonymousClient setParameterEncoding:AFJSONParameterEncoding];
[self.anonymousClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
}
- (void)GET:(NSString *)path parameters:(NSDictionary *)params success:(SuccessCallback)sCallback error:(ErrorCallback)eCallback {
[_secureClient getPath:path
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
DDLogVerbose(@"Success Path: %@ JSON: %@", path, responseObject);
if (sCallback) sCallback(responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[Rest callErrorBlock:eCallback withOperation:operation];
}];
}
@end
模型访问层
@interface Model
+ (Model *)shared;
- (void)appsForLocation:(Location *)location success:(SuccessCallback)success error:(ErrorCallback)error;
@end
@implementation Model
- (void)appsForLocation:(Location *)location success:(SuccessCallback)success error:(ErrorCallback)error {
NSString *path = [NSString stringWithFormat:@"/api/locations/%@/apps/", location.locationId];
[[Rest sharedClient] GET:path parameters:nil success:success error:error];
}
@end
位置是应用程序中的根对象,它将被告知经常刷新。通过UI交互,事件或数据反序列化,refreshApplications将执行以从服务器获取更多数据。同时,应用程序中正在进行的其他请求和事件以获取和发送数据到API是JSON。其中一些对其他端点的GET调用似乎在干扰响应数据。
问题
答案 0 :(得分:4)
我建议您使用Charles代理仔细检查您收到的数据是否正确。有一个试用版可以与注册版相同的工作30天。我的第一个猜测是,你和你的服务器之间存在某种错误的缓存层,或者你的服务器有问题。像查尔斯这样的HTTP代理将允许您确认或拒绝此假设。
This page解释了如何设置Charles来代理来自iOS设备的非HTTPS连接。
答案 1 :(得分:1)