An example of fault tolerance in IOS development

Time:2020-2-13

Preface

Sometimes the value returned by the background server to the client will be null, sometimes “< null >”, and sometimes it will crash if the value is assigned directly and subsequent operations are carried out. The previous processing method is to try not to return null or “< null >” when the background server returns data, but they still return these data from time to time, so the app will flash back from time to time. Once this problem occurs, it is found that they return null or “null” data types after debugging. Because it is an online problem, it is better for them to directly handle the problem fields in the background. As time goes by, I found that this kind of processing method is not desirable. I felt that some fault-tolerant processing should be done on the app side, so I have this article. Next, let me see what fault-tolerant processing methods are available.

The details are as follows:

Write a tool class to determine whether each field is an [nsnull] object, or whether it isisEqual:@"<null>";

If afnetworking is used as the network request, the following variables can be set as yes;


// AFURLResponseSerialization.h
/**
 Whether to remove keys with `NSNull` values from response JSON. Defaults to `NO`.
 */
@property (nonatomic, assign) BOOL removesKeysWithNullValues;

Daniel wrote an nsnull classification: the source address (local download) usage is to drag the class directly into the project. Nullsafe idea: in runtime operation, replace null value with nil, and sending messages to nil pointer will not crash.

I’m mainly talking about the third solution. I downloaded the project from GitHub to the local, and then dragged it to the project. I thought it would solve the problem. Because I wrote a test demo and assigned null to uilabel, it crashed when I didn’t drag it into the library. After dragging the library into the project, I found that the project was running normally. So we think it can be done once and for all, but when we test in the project, we still find that there is a crash. After breaking the debugging point, we find that the data returned from the background is “< null >”, while the nsnull classification only judges whether the data is of nsnull type. If it is of nsnull type, we set it to nil, but the data returned from the background is “< null >”, which is not of nsnull type It will still crash, so I added a judgment on whether it is equal to < null > on the basis of this project. If the condition is true, it will be set to nil, so that we can avoid the problem of crash caused by sending back unwanted data in the background. The modified code is as follows:


- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
 //look up method signature
 NSMethodSignature *signature = [super methodSignatureForSelector:selector];
 if ([signature isEqual:@"<null>"]) {
 
 signature = nil;
 
 }else if ([signature isKindOfClass:[NSNull class]]) {
 
 signature = nil;
 
 }else if (!signature) {
 
 //check implementation cache first
 NSString *selectorString = NSStringFromSelector(selector);
 signature = signatureCache[selectorString];
 if (!signature)
 {
  @synchronized([NSNull class])
  {
  //check again, in case it was resolved while we were waitimg
  signature = signatureCache[selectorString];
  if (!signature)
  {
   //not supported by NSNull, search other classes
   if (signatureCache == nil)
   {
   if ([NSThread isMainThread])
   {
    cacheSignatures();
   }
   else
   {
    dispatch_sync(dispatch_get_main_queue(), ^{
    cacheSignatures();
    });
   }
   }
   
   //find implementation
   for (Class someClass in classList)
   {
   if ([someClass instancesRespondToSelector:selector])
   {
    signature = [someClass instanceMethodSignatureForSelector:selector];
    break;
   }
   }
   
   //cache for next time
   signatureCache[selectorString] = signature ?: [NSNull null];
  }
  }
 }
 }
 return signature;
}

Of course, you can also go to GitHub to download the source address (local download), but you need to add the code shown in the following figure:

Screenshot of code to be added

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.