This is the 10th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

DSYM how do you analyze it?

What is dSYM?

When Xcode compiles the project, we will see a dSYM file with the same name. DSYM is a relay file that holds the address mapping information for hexadecimal functions. The symbols we are debugging will be included in this file, and a new dSYM file will be generated each time we compile the project. Located in the/User / < User name > / Library/Developer/Xcode/Archives directory, for every release we it is necessary to save the corresponding Archives files;

What’s the use of dSYM files?

When our software is packaged in release mode or goes online, we will not see the crash error as intuitively as we did in Xcode. At this time, we need to analyze the crash report file. There will be a log file in iOS device to save the memory address of the function where each application fails. The Organizer of Xcode can export DeviceLog from the iOS device into a crash file. At this time, we can query the corresponding function name and file name of the program in the dSYM file through the error function address. The main precondition is that we need to have dSYM files for the software version, which is why it is necessary to keep Archives files for every release.

How do I match files one to one?

Each xx.app and xx.app.dSYM file has its own UUID, and the crash file also has its own UUID. As long as the UUID of these three files – -, we can use them to resolve the correct error function information.

  • 1. Check thexx.appOf the fileUUID, terminal Enter the command:dwarfdump --uuid xx.app/xx(XX stands for your project name)
  • 2. Check thexx.app.dSYMOf the fileUUID, enter the command in terminal:dwarfdump --uuid xx.app.dSYM
  • 3.crashThe first line in the fileIncident IdentifierIs that thecrashOf the fileUUID.

What are the types of multithreading?

Multithreaded classification

  • pthread
    • A general purpose multithreaded API
    • Suitable for Unix, Linux, Windows and other systems
    • Cross-platform and portable
    • Difficult to use
    • Language: C language
    • Frequency of use: Hardly ever
    • Thread life cycle: managed by the developer
  • NSThread
    • object-oriented
    • Easy to use, can directly operate the thread
    • Language used: OC Language
    • Frequency of use: Occasionally
    • Thread life cycle: managed by the developer
  • GCD
    • Technology to replace NSThread
    • Takes full advantage of the device’s multi-core (automatic)
    • Language: C language
    • Frequency of use: Often
    • Thread life cycle: automatically managed
  • NSOperation
    • Based on the GCD
    • More simple and practical features than the GCD
    • Use more object orientation
    • Language used: OC Language
    • Frequency of use: Often
    • Thread life cycle: automatically managed

Principle of multithreading

At the same time, the CPU can only handle one thread, only one thread at work (execute) multi-threaded concurrent execution, (and) is actually a CPU rapidly between the multiple thread scheduling (switch) if the CPU thread scheduling time fast enough, creates a multi-threaded concurrent execution of illusion to think: if the thread is very, very much, what happens? The CPU will be scheduled between N threads, the CPU will be tired, it will consume a lot of CPU resources and each thread will be scheduled to execute less often.

Advantages of multithreading

Can properly improve the efficiency of program execution can properly improve the utilization of resources (CPU, memory utilization)

Disadvantages of multithreading

Thread take up some memory space (by default, the main thread take up 1 m, the child thread take up 512 KB), a large number of threads, if open will take up a lot of memory space, reduce the performance of the program thread, the more the greater the overhead of CPU scheduling threads on the program design is more complicated: such as communication between threads, multi-threaded data sharing;

GCD versus NSOperation

  • 1.GCDIs the underlying C language that makes up the API, whereasNSOperationQueueAnd the related objects areObjcThe object. inGCDIs executed in the queue byblockStructure of tasks, which is a lightweight data structure; whileOperationAs an object, it provides us with more choices;
  • 2, inNSOperationQueue, we can cancel the task that has been set to be executed at any time (of course, the task that has already started cannot be stopped), andGCDCan’t stop already joinedqueuetheblock(There is, but it takes a lot of complex code);
  • 3,NSOperatioN is an easy way to set up dependencies, so we can have oneOperationIt depends on the other oneOperationIn this case, even twoOperationIn the same parallel queue, but the former will not execute until the latter is finished;
  • 4. Can we bringKVOThe application inNSOperation, you can listen to oneOperationWhether to complete or cancel, this way can be compared toGCDMore effective control over the background tasks we perform;
  • 5, inNSOperationIn, we can setNSOperationthepriorityPriority enables tasks in the same parallel queue to be executed sequentially, while inGCD, we can only distinguish the priority of different task queues, if we want to distinguishblockThe priority of the task, which also requires a lot of complex code;
  • 6. We can do the right thingNSOperationIn this case, add member variables and member methods to increase the reusability of the entire code, which is much easier than simply usingblockTasks are queued for execution with more freedom. Add more custom features on. In general,Operation queueProvides more functionality you need when writing multithreaded programs, and hides a lot of complex code for thread scheduling, thread cancellation and thread priority, providing us with a simple API entry point. As a programming principle, we should always use a high-level, well-encapsulated API whenever possible, and only use the low-level API when necessary. But I think when our requirements can be done with simpler underlying code, it’s neatGCDMight be a better choice,The Operation queueTo give us more choices.
  • 7,NSOperationMore functions are available, see the API.NSOperationQueueIs in theGCDBased on the realization of, it’s justGCDA higher level of abstraction
  • 8, inNSOperationQueueIn, you can build eachNSOperationBetween.
  • 9,NSOperationQueuesupportKVO. Can be used to detectoperationWhether executing (isExecuted), end or not (isFinished), whether to cancel (isCanceld)
  • 10,GCDOnly supportFIFOAndNSOperationQueueYou can adjust the execution order of the queue (by adjustingThe weight).NSOperationQueueCan easily manage concurrent,NSOperationPriority between.
  • conclusion
    • useNSOperationOperations depend on each other, operations need to be suspended, concurrency management, priority control between operations, and limit the number of threads that can be executed at the same time. Let the thread stop/continue waiting at some point.
    • useGCDCase: The general requirement is very simple multithreaded operation, usingGCDAll right, simple and efficient. As a rule of programming, we generally want to use a high-level, well-encapsulated API whenever possible, and only use the low-level API when necessary. When the requirements are simple, conciseGCDMight be a better choice, andOperation queueTo give us more choices.

The downside of singletons

  • Advantages:
    • A class is instantiated only once, providing controlled access to a unique instance
    • Saving system resources
    • A variable number of instances are allowed
  • Disadvantages:
    • A class has only one object, may cause the responsibility is too heavy, to a certain extent the violationSingle responsibility principle
    • Because there is no abstraction layer in the singleton pattern, it is difficult to extend singleton classes
    • The abuse of singleton will bring some negative problems. For example, in order to save resources, the database connection pool object is designed as a singleton class, which may lead to the connection pool overflow due to too many programs sharing the connection pool object. If an instantiated object is not used for a long time, the system will consider it garbage and collect it, resulting in the loss of the object’s state

This section describes how to start the App

App startup Process

  • parsingInfo.plist
  • Load related information, such as a splash screen
  • Sandbox establishment, permission checking
  • Mach-Oloading
  • If it is a binary, look for the part that fits the current CPU category
  • Load all dependenciesMach-OFile (recursive callMach-oLoading method)
  • Locate internal and external pointer references such as strings, functions, etc
  • The execution declaration isattribute(constructor)The C function
  • Loads a method in an extension of a class
  • C++Static object loading, callObjcthe+loadfunction

Program execution

  • mainfunction
  • performUlApplicationMainfunction
  • createUIApplicationobject
  • createUIApplicationDelegateObject and copy
  • Reading a configuration fileinfo.plist, to set some properties of the program launch
  • To create the applicationMain Runloopcycle
  • UlApplicationDelegateObject begins to process listener events
  • After the program starts, the first callapplication.didFinishLaunchingWithOptions:methods
  • Such asIf the info. The plistIs configured in thestoryBoardIs loadedstoryboardfile
  • If it is not configured, it is created from codeUIWindow ->rootViewController– > show

The App starts slowly. What might be the cause?

  • Factors that affect startup performanceAppEach step in the startup process affects startup performance, but some parts of the startup process take very little time, while others are unavoidable. Considering the input-output ratio, we only list the parts we can optimize:mainFactors that affect the time spent before the function
  • The more dynamic libraries are loaded, the slower the startup.
  • ObjCThe more classes, the slower the startup
  • CtheconstructorThe more functions, the slower startup
  • C++The more static objects, the slower startup
  • ObjCthe+loadThe more, the slower the startup experiment proves, inObjCThe more dynamic libraries that need to be loaded for the same number of classes,AppThe slower it starts. Similarly, with the same number of dynamic libraries,ObjCThe more classes,AppThe slower the startup. Need to load the dynamic library from1A rose to10When the user perceives almost no difference from10One, go up to100It will become very obvious when the time comes. In the same way,100A class and1000I mean, it might be hard to detect, but1000A class and10000The differences between the classes begin to become apparent. Again, try not to writeatribute((constrcror))theC functionsAnd try not to use itC++Static object of; As for theObjCthe+loadMethod, it seems that people are used to not using it. In any case, yesdispatch_ _once()Try not to use any of the above methods.main()Factors that affect the time after a function
  • performmain()Time of function
  • performapplicationWillFinishLaunchingThe time-consuming
  • RootViewController andchildViewControllerThe loading,viewAnd itssubviewsThe loadingapplicationWillFinishLaunchingThe time-consuming

What does 0x8badF00d indicate?

  • 0x8badf00d: This code indicates that the application occurred becausewatchdogTimeout byiOSThe termination. Often the application takes too much time to start, stop, or respond to system events.
  • 0xbad22222: Indicates the codeVolPThe application was terminated because it restarted too often
  • Oxdead10ccThis code indicates that the application was terminated because it occupied system resources while running in the background, such as the address book database.
  • Oxdeadfa11: This code indicates that the application was forcibly logged out by the user. According to Apple documentation, forced exit occurs when the user holds down the switch button until a “swipe to shut down” appears, and then holds down the Home button. Forced exits will produce a crash log with 0xDeadFA11 exception code, because most forced exits are caused by the application blocking the interface.

How do I prevent decompilation?

  • Local data encryption: one of the anti-decompile encryption technologies for iOS applications: YesNSUserDefaults.sqliteEncrypts file data to protect accounts and key information
  • URL encoding encryption:iOSApplication of anti-decompile encryption technology two: on the program appearsURLCode encryption to prevent thisURLStatically analyzed
  • Network data encryption:iOSThe third application of anti-decompilation encryption technology: provide encryption scheme for the data transmitted by the client, effectively prevent the interception of data through the network interface
  • Method body, method name advanced obfuscation:iOSApplication of anti-decompile encryption technology four: on the applicationThe method nameandmethodsBody is obfuscated to ensure that the source code cannot be parsed after being reversed
  • Program structure mixed encryption:iOSUse anti-decompilation encryption technology five: the logical structure of the application to shuffle, to ensure that the source code readability to a minimum
  • Use a third-party APP for security hardening

What third party principles or underlying knowledge do you know?

Runtime, Runloop, block, SDWebImage, AFN, YYCache, GCD and other low-level implementation