This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging


My column

  1. Exploring the underlying principles of iOS
  2. Summary of the phase of iOS underlying principle exploration

Let’s start with the problem

In the service logic of a project, after the inspection order is distributed to someone, you need to start the inspection, perform the inspection item by item, and finally submit and save the inspection. The problem is that when the inspection starts, the speed will be very slow. The main performance in the database has 500,000 data (monad). Each inspection item is displayed in the log every 4 seconds between the time the item is written from the template table and the time the inspection item is entered into the intermediate table. However, there will be 100+ lists every day, each list of 30~40 items of different inspection items, you can imagine how slow. Can’t stand the current speed, start to interrupt the point to find the cause of the final location of the problem in the following method:

+ (NSString *)getInspectionContensID { NSString *uuidString = [WJHUUIDManager getUUIDString]; NSString *sql = [NSString stringWithFormat:@" SELECT ID FROM om_on_inspection_entry_mg "]; NSMutableArray *uuidArray = [[WJHDBManager sharedManager] selectWithSql:sql andOneArr:@[@"ID"]]; if (uuidArray.count == 0) { return uuidString; }else { for (NSArray *arr in uuidArray) { if ([[[arr firstObject] objectForKey:@"ID"] isEqualToString:uuidString]) { return [WJHUUIDManager getInspectionContensID]; }else { return uuidString; }}}}Copy the code

Each inspection item requires a unique UUID. This is to prevent the UUID from duplicates. Obviously, the problem is very obvious, half a million levels of data, every time you go through the database, and then compare, obviously, time is wasted on the query.

The static concept

Now, let’s start by showing how static helps me. First, the static variable:

1. On variables: when you declare a local variable as static, you change the way it is stored (its lifetime) so that it becomes a static local variable. That is, memory is allocated for the variable at compile time, and memory is not released until the program exits. Like this, makes the local variable has a memory function, can remember the last time, but because is still a local variables, which can only be used inside the code block unchanged (scope) declared with the static external variables -- -- -- -- -- -- -- all external variable refers to the code block {} variables defined outside, it defaults to static variables, compile time allocated memory, The memory unit is freed at the end of the program. It's also very broad in scope, so the entire file is valid and even other files can refer to it. To limit the scope of some external variables so that they are valid only in this file and cannot be referenced in other files, declare them with the static keyword. Advantages of static variables: 1, save memory. Static variables are stored in one place but are used by all objects. 2. Its value can be updated. 3, can improve the time efficiency. As long as an object updates a static variable once, all objects can access the updated value.Copy the code

The other:

2. On functions: Using static for function definitions affects how functions are linked so that they are not visible to other files and are only available inside the file. Such functions are also called static functions. The advantage of using static functions is that you don't have to worry about interfering with functions of the same name in other files, and it is also a protection mechanism for the function itself. If you want other files to reference a local function, you define it with the keyword extern to indicate that the function is external and can be called by other files. In addition, in files that refer to external functions defined in other files, use extern to declare the external functions to be used.Copy the code

The above is the explanation of it. In short, the direction of my optimization now is that I only query the UUID of these inspection items once and store it, and then directly use it, so that the speed can be determined quickly, and the generated new items will be added to the new storage in time. In this way, we are optimized to the following

Static usage optimization

static NSMutableArray *inspectionUUID; @implementation WJHUUIDManager + (NSMutableArray *)readLocalFile { static dispatch_once_t predicate; dispatch_once(&predicate, ^{ NSString *sql = [NSString stringWithFormat:@" SELECT ID FROM om_on_inspection_entry_mg "]; inspectionUUID = [[WJHDBManager sharedManager] selectWithSql:sql andOneArr:@[@"ID"]]; }); return inspectionUUID; } + (NSString *)getInspectionContensIDWithContentID:(NSMutableArray *)contentID { NSString *uuidString = [WJHUUIDManager  getUUIDString]; if (contentID.count == 0) { inspectionUUID = [WJHUUIDManager readLocalFile]; if (inspectionUUID.count == 0) { return uuidString; }else { if ([inspectionUUID containsObject:uuidString]) { [inspectionUUID addObject:uuidString]; return [WJHUUIDManager getInspectionContensIDWithContentID:inspectionUUID]; }else { return uuidString; } } }else { if ([contentID containsObject:uuidString]) { [contentID addObject:uuidString]; return [WJHUUIDManager getInspectionContensIDWithContentID:contentID]; }else { return uuidString; } } } @endCopy the code

In this way, it is implemented to query the UUID of all inspection items once, and store them for comparison. Finally, the increase in speed is like this. It used to be 3 minutes for the same inspection sheet, but after optimization, it will be 6s. Haha, problem solved.