Network request sending method of dependency relationship, terminal device and storage mediumTechnical Field
The present invention relates to the field of computer technologies, and in particular, to a method for sending a network request based on a dependency relationship, a terminal device, and a storage medium.
Background
When a new page is opened by entering a website, new page resources are often loaded, and the resources are acquired by initiating a network request. In practice, initiation of one network request is often dependent on completion of another request, and since multiple network requests are sent simultaneously each time a resource request is initiated, complex logical relationships exist among the network requests. For network requests with dependencies, the conventional approach is to define various complex internal processing logics to manage the initiation of the network requests, which not only results in a large amount of code redundancy, but also is inconvenient for maintenance.
Thus, there is a need for a method that can simplify handling network requests having dependencies.
Disclosure of Invention
In view of this, embodiments of the present invention provide a method for sending a network request with a dependency relationship, a terminal device, and a storage medium, so as to solve the problem of code redundancy in the existing method for initiating a network request with a dependency relationship.
In a first aspect of the embodiments of the present invention, a method for sending a network request of a dependency relationship is provided, including:
when a user initiates a network request, storing a network request object with a dependency relationship into a predefined array;
defining a callback function of the network request object, and setting that when the current network request is successful, the initiation of the next network request is controlled according to the return result of the current network request object in the callback function;
and acquiring a first network request object in the array and initiating a network request.
In a second aspect of the embodiments of the present invention, there is provided a network request transmission terminal apparatus for dependency relationship, including:
the storage module is used for storing the network request object with the dependency relationship into a predefined array when a user initiates a network request;
the setting module is used for defining a callback function of the network request object, and setting that when the current network request is successful, the initiation of the next network request is controlled according to the return result of the current network request object in the callback function;
and the initiating module is used for acquiring a first network request object in the array and initiating a network request.
In a third aspect of the embodiments of the present invention, there is provided a terminal apparatus, including a memory, a processor, and a computer program stored in the memory and executable on the processor, where the processor executes the computer program to implement the steps of the method according to the first aspect of the embodiments of the present invention.
In a fourth aspect of the embodiments of the present invention, a computer-readable storage medium is provided, in which a computer program is stored, and the computer program, when executed by a processor, implements the steps of the method provided in the first aspect of the embodiments of the present invention.
In a fifth aspect of embodiments of the present invention, a computer program product is provided, the computer program product comprising a computer program that, when executed by one or more processors, performs the steps of the method provided in the first aspect of embodiments of the present invention.
In the embodiment of the invention, all network requests with dependency relationship are put into the same array, each network request is set to have a return result, and the initiation of the next request is controlled, so that the associated initiation of the network requests is realized. The logic relation initiated by different network requests can be simplified, the code redundancy is reduced, and the later maintenance is facilitated.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings needed to be used in the embodiments or the prior art descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
Fig. 1 is a flowchart of an embodiment of a method for sending a network request of a dependency relationship according to the present invention;
fig. 2 is a flowchart of another embodiment of a method for sending a network request of a dependency relationship according to an embodiment of the present invention;
fig. 3 is a schematic structural diagram of a terminal device for sending a network request of a dependency relationship according to an embodiment of the present invention;
fig. 4 is a schematic structural diagram of a terminal device according to an embodiment of the present invention.
Detailed Description
The embodiment of the invention provides a network request sending method, a terminal device and a storage medium of a dependency relationship, which are used for simplifying a network request initiating mode with the dependency relationship.
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is obvious that the embodiments described below are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The first embodiment is as follows:
referring to fig. 1, a schematic flow chart of a network performance parameter acquisition method according to an embodiment of the present invention includes the following steps:
s101, when a user initiates a network request, storing a network request object with a dependency relationship into a predefined array;
the network request is generally a data request sent from a client to a server through a network, and each data request is initiated by sending a corresponding network request object. In the embodiment of the invention, a network request class is defined, a network request object is generated by instantiating the class, and the server-side response data is transmitted back to the client-side through the network request object. Preferably, the network request is a network request in an IOS.
The dependency relationship refers to the successful initiation and response of two network requests, the initiation of one network request depends on the other data, when a common network request is initiated, concurrent requests can be managed through multiple threads, and the requests with the dependency relationship can be initiated in sequence.
In each network request object, there is a callback of the server response result, and each time a network request object initiates a network request, the callback may return response information of the server.
Optionally, a network request class is defined, a sending method is defined in the network request class, and when a user initiates a network request, the network request class is instantiated to generate a network request object. Specifically, a network request class is defined, after a request initiated by a user is monitored, the network request class is instantiated, request parameters are transmitted through a generated network request object, and server response data are transmitted back.
Optionally, a classification of the data is created, and after each network request object is set in the classification to initiate a network request, a request result is returned to a network request initiating place; the rewriting system callback method defines a request result returned by each network request object according to the rewriting system callback method, and controls the next network request object to initiate a calling method. The classification can expand the system array attribute, after the array definition classification, the array elements and the network request objects can realize the request synchronization blocking through a classification definition method, and a dependency relationship network request initiating method is established, and the method can call back the network request result.
S102, defining a callback function of the network request object, and controlling the initiation of the next network request according to the return result of the current network request object when the current network request is successful;
the callback is a callback function, the function called by the function pointer is used for one party to call under a specific condition, and the initiation of the network request can be set in the callback.
When the network request object is generated, a callback type is extended for each network request object for controlling the initiation of the network request. Preferably, the callback method after the network request is rewritten is set to complete the network request, not only the original callback method is executed and the request response result is returned, but also the initiation of the next network request is controlled through the rewritten callback, and the delay time of the initiation is set.
Optionally, the delay duration initiated by the next network request in the array is controlled according to the network request result sent back by the current network request object, where the network request result includes the delay duration; and when the delay time is less than zero, terminating the initiation of the next network request. The response result of the server generally includes request result data, such as request error, response data, request delay, and the like, and according to the returned result, the time length of the delay initiation of the next request is controlled or the sending of the next request is directly stopped. The delay time is a preset time, and can be represented by a value smaller than zero to terminate the initiation of the network request, and a value greater than or equal to zero to represent the network request delay time.
S103, acquiring a first network request object in the array and initiating a network request.
And taking out the first network request object in the array, and calling the sending method of the object to initiate the network request.
Optionally, before initiating the network request, the method further includes: and traversing all elements in the array, judging whether all the elements are of the network request object type, and initiating a network request when all the elements are of the network request object type.
And in the array, acquiring a first network request object according to the array subscript, calling a sending method thereof to initiate a network request, and selecting to initiate a next request or resend a current request or stop sending according to a server-side response result.
In the above steps, the callback method is extended in each network object, and the initiation of the next request is controlled according to the return result of the current object, so that the initiation of the network request with the dependency relationship can be simplified.
Example two:
on the basis of fig. 1, taking program code as an example, a detailed implementation process for processing a network request is described in detail with reference to fig. 2, as follows:
after a user initiates a network request, a client sends request data to a server through a request object, and the server returns response data to the client.
S201, network request objectification encapsulation
When a user initiates a network request, the network request needs to be subjected to objectification encapsulation through a predefined class to generate a network request object. Generally, a network request object carries various request parameters to initiate a request to a server.
Illustratively, a network request class is first created, the class name is 'DYRequest', and a request method, a request path url, a request parameter params, and a callback completion and sending method send are defined in the class. In system callback, the callback completion that calls DYRequest returns the response result to the originating request.
The customized network request class can carry various customized parameters in the network request process, so that the control of each network request object is facilitated.
S202, expanding a system array and creating a classification;
the classification can expand the system array attribute, in the implementation of the invention, the generated network request object can be put into the defined array, and the classification of the array can facilitate the calling of each network request object, thereby controlling the initiation of the network request.
In a predefined array, extending attributes for each network request object, implementing service scheduling logic of the network request, illustratively, creating a classification for a system type array NSArray, and defining a callback block type 'RequestSyncBlock' in the classification, wherein incoming parameters of the callback function include: the network request object, the error of the system error type NSError, the response of the server side and the waiting time of the system time type.
Furthermore, a method ' sendSyncCompletion ' (RequestSyncBlock) completion ' dependent on sending the network request is created, and the current request result is returned through the completion every time the network request succeeds in the array through the method.
And each network request object is set in the classification of the array to return the network request result, so that all the network request objects in the array can be controlled conveniently according to the return result.
S203, expanding the callback type of the network request object;
network requests are initiated through each network request object, and after the server successfully responds, request results can be returned through a callback function of the system. And the callback type of the extended network request object is a control method for extending and defining the next network request call on the basis of the system callback.
Illustratively, in the system request class, a callback block type is defined as section completion of 'RequestSectionBlock', and parameters of the block include: a network request object, a request error and a server response. An attribute section of the 'requestsection block' type is defined for the network request class 'DYRequest'.
Further, the method of network request class attribute 'completion' is rewritten, when the 'completion' is called, a call callback function 'section completion' is set at the same time, and the request itself, the request error and the server response are transmitted. The int type attribute index is defined for the network request class 'DYRequest'. An attribute nextRequest of the type 'DYRequest' is defined for the network request class.
After the classification 'NSArray + Request' of the system array is defined in S202, the callback of the block type is defined in the classification, and after each network Request object is set to initiate a network Request, the Request result is returned to the network Request initiation; rewriting the system callback method, adding new call 'section completion' callback, defining the calling logic which controls the next network request object to initiate according to the request result returned by each network request object.
Where index is used to mark the location of the network request object in the array and nextRequest indicates the next network request object. The rewriting callback function section facilitates control of initiation of the next network request according to the response result.
Specifically, when a network request object for realizing the dependency relationship is sent, the initiation of the next network request is controlled by a method defined in the component classes.
Illustratively, the 'sendSyncCompletion (RequestSyncBlock) completion' method is implemented: first, all elements in the array NSArray are traversed, and the type of the network request object is judged. When the callback is called, the current network request is completed, a completion Block in a 'sendsyncblock' completion 'is called, a request (the current request itself) returned by a callback function' section completion 'is entered, an error of the current request, a response of a server is obtained, a return value is recorded as delay (request delay time), whether a next request' nextRequest 'of the current request' exists is judged, and if the next request 'nextRequest' exists is set according to the delay time, and the waiting time of a send method calling the 'nextRequest' to initiate the request is set. If delay is less than 0, nothing is processed.
Secondly, the subscript of the responded network request in the array is taken out, and the next network request object of the array is taken out. Then, the send method is called, and the first network request object initiates the request.
After the network request object is created, an array is created according to the generated network request object, and an extended callback method is called. When one network request is finished, obtaining the result, and controlling the delay starting or stopping of the next network request according to the result. The above process is cycled until all requests are completed.
Example three:
the above mainly describes a network request method, and a network requested terminal device will be described in detail below.
Fig. 3 is a schematic structural diagram of a terminal device for network request transmission of a dependency relationship, including:
the storage module 310: the network request object with the dependency relationship is stored in a predefined array when a user initiates a network request;
the network request is generally a data request sent from a client to a server through a network, and each data request is initiated by sending a corresponding network request object. In the embodiment of the invention, a network request class is defined, a network request object is generated by instantiating the class, and the server-side response data is transmitted back to the client-side through the network request object. Preferably, the network request is a network request in an IOS. The dependency relationship refers to the successful initiation and response of two network requests, the initiation of one network request depends on the other data, when a common network request is initiated, concurrent requests can be managed through multiple threads, and the requests with the dependency relationship can be initiated in sequence.
Optionally, a network request class is defined, a sending method is defined in the network request class, and when a user initiates a network request, the network request class is instantiated to generate a network request object. Each network request of the user generates a request task in a corresponding object, and once the request initiating method is called, the server end responds to the user request, and then a response result is also transmitted to the initiating request.
Optionally, a classification of the data is created, and after each network request object is set in the classification to initiate a network request, a request result is returned to a network request initiating place; the rewriting system callback method defines a method for controlling the next network request object to initiate calling according to the request result returned by each network request object.
The setting module 320: and the callback function is used for defining the network request object, and when the current network request is successful, the callback function is set to control the initiation of the next network request according to the return value of the current network request object.
The callback is a callback function, the function called by the function pointer is used for one party to call under a specific condition, and the initiation of the network request can be set in the callback. When the network request object is generated, a callback type is extended for each network request object for controlling the initiation of the network request. Preferably, the callback method after the network request is rewritten is used for setting the network request to be completed, the callback is carried out through the original callback method, the request response result is returned, and the initiation of the next network request is controlled through the rewritten callback.
Optionally, the controlling the initiation of the next network request according to the return value of the current network request object further includes:
controlling the delay time length initiated by the next network request in the array according to the network request result sent back by the current network request object, wherein the network request result comprises the delay time; and when the delay time is less than zero, terminating the initiation of the next network request. The response result of the server generally includes request result data, such as request error, response data, request delay, and the like, and according to the returned result, the time length of the delay initiation of the next request is controlled or the sending of the next request is directly stopped.
The initiating module 330: the network request is initiated by the first network request object in the array.
And calling a sending method of a first network request object in the array to initiate the network request.
Optionally, the initiatingmodule 330 includes:
traversing unit: and the network request module is used for traversing all elements in the array, judging whether the elements are of the network request object type, and initiating the network request when all the elements are of the network request object type. Before the network request is initiated, the traversal unit ensures whether all the network requests are of the network request object type, and the system is prevented from crashing when the subsequent type operation is wrong.
The terminal device controls the initiation of the network requests in the array with the dependency relationship by setting the return result of each network request object through the setting module, can simplify the logic relationship among the network requests and is convenient for the initiation of the network requests.
Example four:
fig. 4 is a schematic diagram of a terminal device structure for network request transmission of dependency relationships according to an embodiment of the present invention. The terminal device is a mobile computer device with a touch screen, including but not limited to a smart phone, a smart watch, a notebook, a tablet computer, a POS machine, and even a vehicle-mounted computer. As shown in fig. 4, theserver 4 of this embodiment includes: amemory 410, aprocessor 420, and asystem bus 430, thememory 410 including anexecutable program 4101 stored thereon, it being understood by those skilled in the art that the server architecture shown in FIG. 4 does not constitute a limitation of a server and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
The following specifically describes each constituent component of the terminal apparatus with reference to fig. 4:
thememory 410 may be used to store software programs and modules, and theprocessor 420 performs various functional applications of the terminal and data processing by operating the software programs and modules stored in thememory 410. Thememory 410 may mainly include a storage program area and a storage data area, wherein the storage program area may store an operating system, an application program required by at least one function (such as a sound playing function, an image playing function, etc.), and the like; the storage data area may store data (such as audio data, a phonebook, etc.) created according to the use of the server, and the like. Further, thememory 410 may include high speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid state storage device.
Executable program 4101 containing network request methods onmemory 410, the executable program
The routine 4101 may be divided into one or more modules/units stored in thememory 410 and executed by theprocessor 420 to initiate a network request, and may be a series of computer program instruction segments capable of performing specific functions, which are used to describe the execution of thecomputer program 4101 in theserver 4. For example, thecomputer program 4101 may be divided into a storage module, a setting module, and an initiation module.
Theprocessor 420 is a control center of the server, connects various parts of the entire terminal device using various interfaces and lines, performs various functions of the terminal and processes data by operating or executing software programs and/or modules stored in thememory 410 and calling data stored in thememory 410, thereby integrally monitoring the terminal. Alternatively,processor 420 may include one or more processing units; preferably, theprocessor 420 may integrate an application processor, which mainly handles operating systems, application programs, etc., and a modem processor, which mainly handles wireless communications. It will be appreciated that the modem processor described above may not be integrated intoprocessor 420.
Thesystem bus 430 is used to connect functional units inside the computer, and can transmit data information, address information, and control information, and may be, for example, a PCI bus, an ISA bus, a VESA bus, etc. The instructions of theprocessor 420 are transmitted to thememory 410 through the bus, thememory 410 feeds data back to theprocessor 420, and thesystem bus 430 is responsible for data and instruction interaction between theprocessor 420 and thememory 410. Of course, thesystem bus 430 may also access other devices such as network interfaces, display devices, and the like.
The terminal device at least comprises a network card, an output device and the like, and other components are not described in detail herein.
In this embodiment of the present invention, the executable program executed by theterminal device 420 included in the server specifically includes:
a method for sending a network request of a dependency relationship comprises the following steps:
when a user initiates a network request, storing a network request object with a dependency relationship into a predefined array;
defining a callback function of the network request object, and setting that when the current network request is successful, the initiation of the next network request is controlled according to the return result of the current network request object in the callback function;
and acquiring a first network request object in the array and initiating a network request.
Further, when the user initiates a network request, storing the network request object with the dependency relationship in a predefined array further includes:
defining a network request class, defining a sending method in the network request class, and instantiating the network request class to generate a network request object when a user initiates a network request.
Further, the storing the network request object with the dependency relationship into a predefined array further includes:
creating the classification of the data, and returning a request result to a network request initiating position after each network request object is set to initiate a network request in the classification; the rewriting system callback method defines a method for controlling the next network request object to initiate calling according to the request result returned by each network request object.
Further, the controlling the initiation of the next network request according to the return value of the current network request object further includes:
controlling the delay time length initiated by the next network request in the array according to the network request result sent back by the current network request object, wherein the network request result comprises the delay time; terminating initiation of a next network request when the delay time is less than zero
Further, before the acquiring the first network request object in the array initiates the network request, the method further includes:
and traversing all elements in the array, and judging whether all the elements are of the network request object type.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
It is clear to those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described systems, apparatuses and units may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the above embodiments, the descriptions of the respective embodiments have respective emphasis, and reference may be made to the related descriptions of other embodiments for parts that are not described or illustrated in a certain embodiment.
The above-mentioned embodiments are only used for illustrating the technical solutions of the present invention, and not for limiting the same; although the present invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions of the embodiments of the present invention.