1. The process of HTTP link disconnection?
Step 1: TCP connection setup: three-way handshake
HTTP is an application-layer protocol, and its work requires the support of data-layer protocols, most often paired with TCP (the application layer and data layer are part of the OSI seven-layer model, which will be discussed later). The TCP protocol, called the Data Transfer Protocol, is reliable, connection-oriented, and byte stream oriented.
Connection-oriented: Establish a connection before communicating, and ensure that both parties are online. Reliable transmission: Data will not be lost under normal network conditions. Byte stream – oriented: flexible transmission, but TCP transmission exists sticky packet problem, no obvious data convention.
Before sending a request, you need to establish a TCP connection. The process of establishing a TCP connection is simply to send three messages between the client and the server to ensure the establishment of the connection. This process is called the triple handshake.
Step 2: The browser sends the request
After the TCP connection is established, the client can send a request packet to the server
Request messages are divided into request lines, request headers, empty lines, and request bodies. The server obtains information from the client through the request lines and request headers, and obtains data from the client through the data in the request body.
Step 3: Reply
After receiving and confirming the request packet from the client. The server sends response packets to the client
The response message consists of a stateful line, a response header, a blank line and a response body. The server uses the status line and the response header to tell the client the status of the request and how to process the data. The real data is transmitted to the client in the response body.
Step 4: Disconnect the TCP connection
When the request is complete, the TCP connection needs to be disconnected
The process of disconnection is simply to send four messages between the client and the server to ensure that the connection is broken, so it is called quadruple wave.
Extension:
One-way request HTTP requests are one-way requests that can only be initiated by the client and responded by the server. (If you need two-way requests, use sockets.)
HTTP is an application layer protocol, so its data transmission is based on TCP protocol.
Stateless HTTP requests are stateless, meaning they have no memory and cannot retrieve the contents of previous requests or responses. At first, this simple mode can speed up the processing speed and ensure the stability of the protocol, but with the development of the application, this stateless mode will make our business implementation become troublesome, for example, to save the user login state, we have to use the database to achieve. Therefore, in order to achieve the maintenance of state, Cookie technology is introduced to manage state.
Four, no connection HTTP protocol can not save the connection state, each connection only processing a request, run out that is broken, so as to save transmission time, improve the concurrency. After a TCP connection is disconnected, the client and server are like strangers and the next time they send a request, they have to re-establish the connection. Connection: keep-alive In HTTP1.1, Connection: keep-alive in HTTP1.1, Connection: keep-alive in HTTP1.1, Connection: keep-alive in HTTP1.1
2. Deep copy and shallow copy
Deep copy and shallow copy examples?
Deep copy: is a copy of the object itself; Shallow copy: is a copy of a pointer;
In oc, a pointer to a parent class can point to an object of a subclass, and that’s one of the properties of polymorphism to declare an NSString to point to an NSMutableString, which is perfectly fine, because the parent of NSMutableString is NSString. NSMutableString is an object that can be changed, and if we decorate it with strong, NSString strongly references NSMutableString. If we modify the NSMutableString object somewhere else, then the value of the NSString will change.
About the copy modifier
Copy NSString -> this is a shallow copy, but because it is immutable, the later value will not change;
MutableCopy -> This is a deep copy, but it’s a mutable object NSMutableString;
Copy NSMutableString -> This is a deep copy, copy an immutable object;
NSmutableString = NSmutableString = NSmutableString ();
Conclusion:
MutableCopy on an object, whether it’s mutable or immutable is a deep copy, and the copied object is mutable;
Copy an object, and everything that comes out of it is immutable.
For a system of non-container objects, we can think of copy as a pointer copy (shallow copy) and mutableCopy as an object copy (deep copy) if an immutable object is copied. If it is a copy of a mutable object, it is a deep copy, but the object returned by copy is immutable.
NSArray, NSDictionary, etc. The results discussed above also apply to the container classes themselves, where the objects in the container change after replication.
// Copy returns an immutable object and mutablecopy returns a mutable object
NSArray *array1 = [NSArray arrayWithObjects:@"a".@"b".@"c".nil];
NSArray *arrayCopy1 = [array1 copy];
//arrayCopy1 is the same NSArray object as array (pointing to the same object), including the elements in the array that point to the same pointer
NSLog(@"array1 retain count: %d",[array1 retainCount]);
NSLog(@"array1 retain count: %d",[arrayCopy1 retainCount]);
NSMutableArray *mArrayCopy1 = [array1 mutableCopy];
Copy the code
MArrayCopy1 is a mutable copy of Array1, pointing to different objects, but with elements that point to the same objects as those in Array1.
MArrayCopy1 can also modify its own object [mArrayCopy1 addObject:@”de”];
[mArrayCopy1 removeObjectAtIndex:0]; Array1 and arrayCopy1 are pointer copies, while mArrayCopy1 is object copies. MArrayCopy1 can also change elements within a period: delete or add. Note, however, that the contents of the elements in the container are pointer copies.
NSArray *mArray1 = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"a"].@"b".@"c".nil];
NSArray *mArrayCopy2 = [mArray1 copy];
NSMutableArray *mArrayMCopy1 = [mArray1 mutableCopy];
NSMutableString *testString = [mArray1 objectAtIndex:0];
[testString appendString:@" tail"];
NSLog(@ "% @ % @ - % @",mArray1,mArrayMCopy1,mArrayCopy2); Results: mArray1, mArrayMCopy1 mArrayCopy2 three the first element of an array of all changed!Copy the code
Add: comments from developers
Interview when I will sometimes ask about copy and mutableCopy, the candidate almost 100% said you are saying deep copy and shallow copy…. I would say no!
> < span style = “max-width: 100%; clear: both;
NSArray and other types of copy do not actually have a copy, or at best a reference, because the copy method returns only self, which is an optimization for memory.
2, The copy of NSMutableArray does copy the new NSArray object, but it’s not a deep copy, because it’s just a shallow copy of an NSArray, and the contents are still the contents of NSMutableArray. == == =
NSArray and NSMutableArray mutableCopy are similar to NSMutableArray, except that the result is NSMutableArray.
4. The above statement generally applies only to the types provided by Foundation, but most of the time it does not apply to classes written by yourself — think about how your own classes implement the NSCopying protocol. Is the NSMutableCopying protocol implemented?
So there is no such thing as a deep copy of ObjC. To achieve true deep copy, you basically have to rely on serialization + deserialization to get bottom-deep copy results.
If you say that we all understand the truth, deep copy, shallow copy is just a name, then I can only say that you are too not rigorous, the official document has never taught so; And it doesn’t make it easy for beginners to understand and learn from other languages, such as Java.
Therefore, it is recommended to be more rigorous and can be called reference copy and shallow copy, deep copy is rarely used; Or two antonyms can be called true copy and false copy.
3. Load is different from initialize
What is the difference between load and initialize methods, and the order in which they are called in subclasses, superclasses, and classes?
+(void)load
1, +load method load order: parent > subclass > classification (load method will be loaded) note: (if there is A class A, B, the order depends on the order of A, B added to the project), possible results: (Parent > Child > Category A> Category B) or (Parent > Child > Category B> Category A)
2, the +load method will not be overridden (for example, if there is A parent class, A child class, class A, class B, all four loads will be loaded).
3. The +load method is called before main
+(void)initialize
1. Classes (subclasses have no initialize method, parent classes exist or have no 1initialize method)
2, classification > subclass (multiple categories depending on the compile order, only one exists)
Class initialize > subclass
4, Parent class (all classes have no initialize method)
Conclusion + initialize:
Class initialize overwrites the parent class if the parent class has its own class
Class + initialize overwrites the parent class
Subclass + initialize does not overwrite classes
The parent class + initialize may not be called, as it is possible that the parent class overrides it
5. Occurs after main.
4. Method call sequence with the same name
What is the call order of methods with the same name in subclass, superclass, or class?
The load,initialize methods call source code analysis
Note: the +load method is called directly based on the method address, not through objc_msgSend (isa and superclass), so there is no problem with method overwriting.
5. Event response chain
Event response chain (the same controller has three views, how to determine whether they have the same parent view)
When the iOS system detects a finger Touch operation, it will package it into a UIEvent object and put it into the event queue of the current active Application. The singleton UIApplication will pull the touch event from the event queue and pass it to the singleton UIWindow to handle it, and the UIWindow object will first use hitTest:withEvent: The hit-test View () method looks for the View at the initial point of the Touch operation, that is, the View to which the Touch event needs to be passed.
UIAppliction –> UIWiondw –> find the most appropriate control to handle the event –> The control calls the Touches method –> Checks whether the Touches method is implemented –> Does not implement the event by default to the last responder –> Find the last responder.
UIResponder is the base class for all response objects, in which interfaces are defined to handle the various events described above. The familiar UIApplication, UIViewController, UIWindow, and all UIKit classes that inherit from UIView inherit directly or indirectly from UIResponder, so their instances are responder objects that can form a responder chain.
// How do I get the superview
UIResponder *nextResponder = gView.nextResponder;
NSMutableString *p = [NSMutableString stringWithString:@ "--"];
while (nextResponder) {
NSLog(@ % @ % @ "", p, NSStringFromClass([nextResponder class]));
[p appendString:@ "--"];
nextResponder = nextResponder.nextResponder;
}
Copy the code
The nextResponder points to the parent view if there’s a parent view and the controller root view points to the controller;
If the controller is in the navigation controller, the relevant display view pointing to the navigation controller finally points to the navigation controller;
If it’s the root controller it points to UIWindow;
UIWindow’s nexResponder points to UIApplication and finally points to AppDelegate.
6. TCP packet loss
Does TCP lose packets? How to deal with it? Network disconnection Start a meeting disconnect the connection or wait for a long time, if the network disconnection?
TCP in the unreliable network to achieve reliable transmission, there will be packet loss. TCP is a “stream” protocol, a detailed packet will be split into several packets by TCP upload, will also be the small package into a large upload, this is to say TCP sticky packet and unpacket problems.
Summary of TCP packet loss
7. Automatic release pool
When was the auto release pool created and released, and when did the child thread create the release?
The main thread runloop is enabled by default, and the child thread runloop is disabled by default. This means that autoReleasepool will not be created in the child thread, so we need to create an autoreleasepool in the child thread. (The class methods used in the child thread are all autoRelease, so there is no pool torelease, which means there is no way torelease later, causing a memory leak.)
If an event is generated in the main thread then the runloop goes back to create the Autoreleasepool. This explains why the autoreleasepool is not created in the child thread because the runloop of the child thread is closed by default. So it doesn’t create autoReleasepool automatically, so you need to add it manually.
If you generate a child thread, create an automatic release pool as soon as the thread starts executing, otherwise you will leak memory. Because child threads cannot access the main thread’s auto-release pool.
8. Computer compilation process
Source files: Load. H,. M, and. CPP files
Preprocessing: replace macros, delete comments, expand header files, generate.i files
Compilation: converts.i files to assembly language, producing.s files
Assembly: To convert assembly files into machine code files, producing.o files
Link: References to other libraries in the.o file to produce the final executable
Dyld loading process