CN116974723A - Global event bus control method and device, electronic equipment and readable storage medium - Google Patents

Global event bus control method and device, electronic equipment and readable storage medium Download PDF

Info

Publication number
CN116974723A
CN116974723A CN202310965048.5A CN202310965048A CN116974723A CN 116974723 A CN116974723 A CN 116974723A CN 202310965048 A CN202310965048 A CN 202310965048A CN 116974723 A CN116974723 A CN 116974723A
Authority
CN
China
Prior art keywords
event
global
global event
sticky
label
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202310965048.5A
Other languages
Chinese (zh)
Inventor
张鑫
修怀忠
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Chongqing Selis Phoenix Intelligent Innovation Technology Co ltd
Original Assignee
Chongqing Seres New Energy Automobile Design Institute Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Chongqing Seres New Energy Automobile Design Institute Co Ltd filed Critical Chongqing Seres New Energy Automobile Design Institute Co Ltd
Priority to CN202310965048.5A priority Critical patent/CN116974723A/en
Publication of CN116974723A publication Critical patent/CN116974723A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
    • G06F9/485Task life-cycle, e.g. stopping, restarting, resuming execution
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/546Message passing systems or structures, e.g. queues

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Debugging And Monitoring (AREA)

Abstract

The application relates to the technical field of computers, and provides a global event bus control method, a global event bus control device, electronic equipment and a readable storage medium. The method comprises the following steps: monitoring global events of the application program; monitoring the generation of a global event, and acquiring an event name and a classification label of the global event based on an acquisition function in a constructed global event bus, wherein the classification label comprises a sticky label and a non-sticky label, the sticky label represents the global event as the sticky event, and the non-sticky label represents the global event as the non-sticky event; determining a receiver of the global event and a corresponding life cycle based on a receiving function in the global event bus; the receiver is controlled to receive the global event based on the life cycle and the classification label. The global event bus control method provided by the application has the perception capability on the life cycle of the global event, has fewer related functions, does not need to add custom event classes when the global event is newly added, and improves the maintainability of the global event bus.

Description

Global event bus control method and device, electronic equipment and readable storage medium
Technical Field
The present application relates to the field of computer technologies, and in particular, to a global event bus control method, a global event bus control device, an electronic device, and a readable storage medium.
Background
In the prior art, an application program includes a global event bus for transmitting and receiving events between different pages, where the global event bus may be implemented by, for example, eventBus and LiveDataBus based on an official component library, where EventBus is an implementation method of an event bus with an open source framework, liveDataBus is a message communication framework similar to EventBus based on LiveData implementation, and LiveData is a data storage class capable of observing and monitoring. Wherein the global event bus implements event notification based on the observer pattern of LiveData.
However, when the global event bus is constructed according to the EventBus and the global event is controlled through the global event bus, the global event bus distributes the global event through the annotation method parameter types, so that each global event has a corresponding data class, and as time goes on, an application program iterates or a system is upgraded, the data classes become more and more, so that the maintenance of the data classes by the application program is difficult, and when a receiver is too many or a plurality of same parameters exist, the message flows for transmitting the global event are difficult to distinguish.
For the implementation method of the LiveDataBus, no method used as event notification exists in the LiveData, a sticky event is generated when the global event bus is constructed through the LiveDataBus to control the global event, the sticky event is represented as a new receiver can receive the notification of the data information related to the last global event, and the global event bus constructed according to the LiveData in the prior art cannot process the sticky event on the premise of conforming to the design thought that the LiveData has life cycle perception capability.
Disclosure of Invention
In view of the above, the embodiments of the present application provide a global event bus control method, apparatus, electronic device, and readable storage medium, so as to solve the problem in the prior art that the application program is difficult to maintain the data class.
In a first aspect of an embodiment of the present application, there is provided a global event bus control method, including:
monitoring global events of the application program;
monitoring the generation of a global event, and acquiring an event name and a classification label of the global event based on an acquisition function in a constructed global event bus, wherein the classification label comprises a sticky label and a non-sticky label, the sticky label represents the global event as the sticky event, and the non-sticky label represents the global event as the non-sticky event;
determining a receiver of the global event and a corresponding life cycle based on a receiving function in the global event bus;
the receiver is controlled to receive the global event based on the life cycle and the classification label.
In a second aspect of the embodiment of the present application, there is provided a global event bus control device, including:
the monitoring module is configured to monitor global events of the application program;
the acquisition module is configured to monitor the generation of the global event, acquire the event name and the classification label of the global event based on the acquisition function in the constructed global event bus, wherein the classification label comprises a sticky label and a non-sticky label, the sticky label represents that the global event is a sticky event, and the non-sticky label represents that the global event is a non-sticky event;
the determining module is configured to determine a receiver of the global event and a corresponding life cycle based on the receiving function in the global event bus;
and the control module is configured to control the receiving party to receive the global event based on the life cycle and the classification label.
In a third aspect of the embodiments of the present application, there is provided an electronic device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, the processor implementing the steps of the above method when executing the computer program.
In a fourth aspect of the embodiments of the present application, there is provided a readable storage medium storing a computer program which, when executed by a processor, implements the steps of the above method.
Compared with the prior art, the embodiment of the application has the beneficial effects that: the life cycle of the global event and the life cycle of the application program where the global event is located are bundled according to the constructed global event bus, so that the global performance in the global event bus is realized, the perception capability of the life cycle of the global event is provided, and the global event can be stopped being received when the application program is returned to the background or the page is invisible. In addition, the global event bus control method provided by the embodiment of the application has fewer related functions, custom event classes are not required to be added when a global event is newly added, and the maintainability of the global event bus is improved.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings used in the embodiments or the description of the prior art will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a schematic view of an application scenario according to an embodiment of the present application;
FIG. 2 is a flow chart of a global event bus control method according to an embodiment of the present application;
FIG. 3 is a schematic diagram of a structure for building a global event bus according to an embodiment of the present application;
FIG. 4 is a schematic diagram of a global event bus control device according to an embodiment of the present application;
fig. 5 is a schematic diagram of an electronic device according to an embodiment of the present application.
Detailed Description
In the following description, for purposes of explanation and not limitation, specific details are set forth such as the particular system architecture, techniques, etc., in order to provide a thorough understanding of the embodiments of the present application. It will be apparent, however, to one skilled in the art that the present application may be practiced in other embodiments that depart from these specific details. In other instances, detailed descriptions of well-known systems, devices, circuits, and methods are omitted so as not to obscure the description of the present application with unnecessary detail.
A display control method and apparatus according to embodiments of the present application will be described in detail with reference to the accompanying drawings.
Fig. 1 is a schematic view of an application scenario according to an embodiment of the present application. The application scenario may include a first terminal device 101, a second terminal device 102, and a third terminal device 103, a server 104, and a network 105.
The first terminal device 101, the second terminal device 102, and the third terminal device 103 may be hardware or software. When the first, second, and third terminal devices 101, 102, 103 are hardware, they may be various electronic devices having a display screen and supporting communication with the server 104, including but not limited to smartphones, tablets, laptop and desktop computers, etc.; when the first terminal apparatus 101, the second terminal apparatus 102, and the third terminal apparatus 103 are software, they may be installed in the electronic apparatus as described above. The first terminal device 101, the second terminal device 102, and the third terminal device 103 may be implemented as a plurality of software or software modules, or may be implemented as a single software or software module, which is not limited in this embodiment of the present application. Further, various applications, such as a data processing application, an instant messaging tool, social platform software, a search class application, a shopping class application, and the like, may be installed on the first terminal device 101, the second terminal device 102, and the third terminal device 103.
The server 104 may be a server that provides various services, for example, a background server that receives a request transmitted from a terminal device with which communication connection is established, and the background server may perform processing such as receiving and analyzing the request transmitted from the terminal device and generate a processing result. The server 104 may be a server, a server cluster formed by a plurality of servers, or a cloud computing service center, which is not limited in this embodiment of the present application.
The server 104 may be hardware or software. When the server 104 is hardware, it may be various electronic devices that provide various services to the first terminal device 101, the second terminal device 102, and the third terminal device 103. When the server 104 is software, it may be a plurality of software or software modules providing various services to the first terminal device 101, the second terminal device 102, and the third terminal device 103, or may be a single software or software module providing various services to the first terminal device 101, the second terminal device 102, and the third terminal device 103, which is not limited in this embodiment of the present application.
The network 105 may be a wired network using coaxial cable, twisted pair and optical fiber connection, or may be a wireless network that can implement interconnection of various communication devices without wiring, for example, bluetooth (Bluetooth), near field communication (Near Field Communication, NFC), infrared (Infrared), etc., which are not limited by the embodiment of the present application.
It should be noted that the specific types, numbers and combinations of the first terminal device 101, the second terminal device 102, the third terminal device 103, the server 104 and the network 105 may be adjusted according to the actual requirements of the application scenario, which is not limited in the embodiment of the present application.
In the embodiment of the application, the transmission of the global event is realized through the data stream with the type of ShareFlow, wherein one ShareFlow data stream corresponds to one global event, and the data stream corresponds to one global event sender and a plurality of global event receivers. The global event comprises a sticky event and a non-sticky event, all sticky events are stored in a sticky event set, all non-sticky events are stored in a non-sticky event set, and the data flow can send data information of the global event from the sticky event set or the non-sticky event set to a receiver, wherein the receiver can be a plurality of receivers or a single receiver. The application manages all ShareFlow data flows and corresponding global events by realizing a global ViewModel, and the global event bus is developed based on Kotlen.
The ViewModel is a data storage component with the perception capability of the host later in life, and can be understood that the ViewModel can be used for storing data and still exists in the Activity because of the abnormal destruction and re-creation.
Fig. 2 is a flow chart of a global event bus control method according to an embodiment of the present application. As shown in fig. 2, the global event bus control method includes the steps of:
s201, monitoring global events of an application program;
s202, monitoring the generation of global events, and acquiring event names and classification labels of the global events based on acquisition functions in a constructed global event bus;
s203, determining a receiver of the global event and a corresponding life cycle based on a receiving function in the global event bus;
s204, controlling the receiving party to receive the global event based on the life cycle and the classification label.
The global event bus control method of fig. 2 may be performed by any of the terminal devices of fig. 1, and the global event may be transmitted to any of the terminal devices by the server or transmitted to the server by any of the terminal devices.
The classification labels comprise sticky labels and non-sticky labels, the sticky labels represent global events as sticky events, and the non-sticky labels represent the global events as non-sticky events.
In some embodiments, the global event of the application is snooped via a global event bus. When the occurrence of the global event is monitored, the time name and the classification label of the global event are acquired according to the corresponding acquisition function in the constructed global event bus.
The classification labels comprise sticky labels and non-sticky labels, the sticky labels are used for representing that the global event is a sticky event, and the non-sticky labels are used for representing that the global event is a non-sticky event. The classification tag may be a parameter, for example, may be iscicky, and the iscicky classification tag may be set on the receiver side, so as to distinguish the category of the global event when the global event is broadcast.
And determining according to a receiving function in the global event bus, wherein the acquiring function can be named getEventFlow, comprises two parameters, namely event name eventName and classification label isSticky, judging whether the global event is a sticky event according to the isSticky, acquiring specific data information of the global event in a sticky data stream through the event name if the global event is the sticky event, and acquiring specific data information of the global event in a non-sticky data stream through the event name if the global event is not the sticky event. If the global event does not exist in the sticky data stream and the non-sticky data stream, a new global event is generated, and the new global event is stored in a sticky event set or a non-sticky event set according to the classification label of the global event.
The method comprises the steps that a sticky event set is used for storing data information corresponding to a global event for providing a sticky label, a non-sticky event set is used for storing data information corresponding to a global event for providing a non-sticky label, the data types of the sticky event set and the non-sticky event set are ConcurrentHashMap types, so that multithread data synchronization can be guaranteed, in the sticky event set and the non-sticky event set, keys are used for representing event names of the global event, and values are used for representing a MutableSertredFlow event.
ConcurrentHashMap is a hash table supporting high concurrency update and query, realizes ConcurrentMap interface, and allows multiple threads to read and write at the same time.
The construction method of the MutableSeredFlow event comprises three parameters, which can be respectively set as replay, extraBufferCapacity and onBufferOverflow. The replay is the replay quantity, that is, the quantity of old data information received by a new receiver is set, extraBufferCapacity is a threshold value of the maximum quantity of data information buffered in the data stream except for the replay quantity, onbufferflow is a processing mode of the received data information after exceeding the threshold value of the maximum quantity.
The above embodiment is a flow of receiving global events through a receiving function, and when the global event bus is actually applied to control the global events, a proxy mechanism can be further set on the receiving function, so that a user can more simply implement the flow of receiving the global events through the proxy mechanism. The method comprises the following steps:
A. an extended method of defining a LiftCyclowner observes an event observeEvent for receiving a global event, the method comprising four parameters, isSticky, minActiveState, dispatcher and onReceived, respectively, wherein isSticky is used to indicate whether the global event is a sticky event, minActiveState is used to indicate a minimum lifecycle state, dispatcher is used to indicate a processing event coroutine type, and onReceived is used to indicate a receive event callback. The method obtains a globally unique EventViewModel through a getapplication ViewModelStoreOwner class getapplication ViewModel and calls an observeEvent method of the globally unique EventViewModel, wherein the class name of the value class is used as the event name of the global event. The method is suitable for sending the custom event class. In the method, the default value of the isckiy is set as false, the default value of the minActiveState is set as life. State. RESUMED, the default value of the dispatcher is set as dispatchers. Main. Im media main protocol, and the parameters can not be set when the method is used;
B. a method for defining the same name extension (observeEvent) of a life cycle Owner is different from the same name method in that a eventName parameter is added, the parameter is used for customizing an event name, and the method is generally used for transmitting only one basic data type in the data information of a global event, so that the global event of an event class does not need to be defined independently.
In an exemplary embodiment of the present application, the number of replays corresponding to the sticky data stream and the non-sticky data stream is set to 1, and since the characteristic of the sticky event is that the data information of the last global event is needed, when the receiver receives the sticky event, the data information of the last global event is repeatedly received according to the number of replays, and when the receiver receives the non-sticky event, the receiver receives the data information corresponding to the global event and clears the data information in the corresponding data stream, so that the receiver cannot repeatedly receive the data information.
In an exemplary embodiment of the present application, a receiver of a global event and a corresponding life cycle are determined according to a receiving function in a global event bus, where the receiving function includes six parameters, which are respectively a life cycle holder, a minimum life cycle state, a coroutine type where callback event data is processed, an event name, data information, and callback data information for the receiver.
Wherein the life cycle holder can be composed of Activity, fragment fragments and Dialog dialogs, and the life cycle holder and minimum life cycle state are used to control the life cycle. The receiving function repeats the life cycle repeat and the minimum life cycle through the life cycle expanding method, so that the life cycle of the global event is controlled.
The repeatOnLifecycle principle is a life cycle management principle used in application programs of an android system, and is mainly used for ensuring that the application programs can correctly execute corresponding operations in different life cycle stages, so that the stability and reliability of the application programs are improved. The core idea is to use a repetitive lifecycle management mode in the application, meaning that in each lifecycle stage the application performs the corresponding operation, ensuring that the application can perform the corresponding operation correctly in the different lifecycle stages.
The control of the life cycle of the global event is specifically: when the repeatOnLifecycle method detects that the life cycle of the current global event is not greater than the minimum life cycle state, the control receiver stops receiving the global event, namely, the receiver is canceled from continuously receiving the global event, and when the life cycle of the current global event is greater than the minimum life cycle state, the cooperative process is restarted, namely, the restart receiver receives the global event, and the receiver immediately receives the existing data in the ShareFlow after restarting.
Coroutine is a computer program component that can be suspended and resumed during execution without losing its state. The cooperative thread is a lightweight thread, a plurality of independent execution flows can be realized in a single thread, and different tasks can be switched through the cooperative thread, so that the concurrent execution of the tasks is realized.
In some embodiments, the receiving function acquires the corresponding global event by calling the acquiring function, calls a collection method of the MutableSeredFlow of the event class to receive the data information of the global event, sends the data information to a module for calling back the data information for the receiving party to call the data information of the receiving global event and call back to the receiving party function, and processes the data information.
The function that receives the data information of the global event and recalls the data information to the receiver may be set to include two parameters, namely a value and an onReceived, where the value is used to receive the data information of the global event, and the onReceived is used to recall the data information to the receiver. The onReceived is a customized fun interface class, which is marked as onReceived < T >, T is general data, a specific data type is represented, and the implementation of the class can be represented as fun interface OnReceived < T >: (T) - > Unit, and can be simply understood as a unilateral method interface.
According to the technical scheme provided by the embodiment of the application, the life cycle of the global event and the life cycle of the application program where the global event is located can be bundled according to the constructed global event bus, so that the global performance in the global event bus is realized, the perception capability of the life cycle of the global event is provided, and the global event can be stopped being received when the application program is returned to the background or the page is invisible.
In some embodiments, after acquiring the event name and the class label of the global event based on the acquisition function in the constructed global event bus, the method further comprises:
judging whether a global event exists in the corresponding event set or not based on the event name and the classification label;
if the global event does not exist, acquiring data information of the global event, and creating the global event in an event set according to the data information;
if so, invoking a global event in the corresponding event set.
The event set comprises a sticky event set and a non-sticky event set, wherein the sticky event set is used for storing all sticky events, and the non-sticky event set is used for storing all non-sticky events.
And positioning to the corresponding event set according to the classification label of the global event, for example, determining that the event set corresponding to the current global event is a sticky event set if the classification label is a sticky label. Meanwhile, according to the event name of the global event, whether the data information of the global event exists in the event set is judged, for example, the data information corresponding to the event name is queried in the event set, and if the corresponding data information can be queried in the corresponding event set according to the event name, the data information is called to prepare to send the data information of the global event to a receiver.
If the data information corresponding to the event name cannot be queried, the data information of the global event is obtained, and the global event is created in the corresponding event set according to the corresponding event name and the data information so as to control the corresponding event set to store the global event, so that the global event can be provided to a receiver through the corresponding event set, and the data information is stored in the corresponding event set, and therefore the data information can be directly transmitted to the receiver through the corresponding event set when the global event is received again.
According to the technical scheme provided by the embodiment of the application, the sticky event set and the non-sticky event set can store and provide the corresponding sticky event or non-sticky event, so that when the global event is received again, the data information corresponding to the event name can be directly transmitted to the receiver through the corresponding event set, the step of acquiring is saved, and the processing efficiency of the global event is improved.
In some embodiments, transmitting data information for a global event based on a transmit function includes:
judging whether delay time exists in the global event or not based on the data information;
if the data exists, acquiring a corresponding delay event data class through a global lifecycle management class function based on delay time, and calling a sending function to send data information based on the delay event data class;
and if not, setting the delay time to be zero, and calling a sending function to send data information based on the delay event data class.
Wherein the transmission function further comprises a delay time.
In an exemplary embodiment of the present application, the sending function is denoted as postEventDelay, where the sending function includes three parameters, which are eventName, value and delayTimeMillis, eventName is an event name of a global event, value is data information of the global event, delayTimeMillis is delay time, and units are ms. When the delay time is set to be none, namely set to be 0, the global event is not delayed to be sent, and when the set delay time is not 0, the corresponding global event is sent according to the set delay time.
In the above example, the sending function obtains the sticky event and the non-sticky event through the obtaining function, and calls the emit method of the MutableSeredFlow to send the data information of the global event. The use of emit is to transfer the content of a sub-component to a parent component, including the data, methods, etc. of the sub-component.
The above embodiment is a flow of specifically implementing global event sending through a sending function, and when the global event bus is actually applied to implement control of the global event, a proxy mechanism may be further set for the sending function, so that a user can implement the flow of sending the global event more simply through the proxy mechanism. The method comprises the following steps:
A. a postEventDelay method is defined for sending a global event comprising a delay time. The method comprises two parameters, namely a value and a delay time Millis, wherein the value is general data, and is data information for sending global events, and the delay time Millis used for indicating to send the global events according to delay time. The sending method obtains a globally unique EventViewModel through a getApplameViewModelStoreOwner corresponding to an ApplameViewModel, and calls a postEventDelay method of the globally unique EventViewModel, wherein the class name of the value class is used as an event name corresponding to a global event, and the method is suitable for sending a custom event class;
B. a postEvent method is defined for sending global events without delay time. The method comprises the steps that a parameter is value, the value is general data, and the data of a transmitted event is obtained through a getapplication view model of an application view model store Owner class, and a postEventDelay method is called, wherein the class name of the value class is used as an event name corresponding to the global event, the value corresponding to delayTimeMillis set to 0, and the method is suitable for transmitting the custom event class;
C. a homonymous method postEventDelay is defined, and the method is different from the homonymous method in that a eventName parameter is added to the homonymous method, and the parameter is used for customizing the event name of the global event. The method is generally used for transmitting only one basic data type in the data information of the global event, so that the global event of an event class is not required to be defined independently;
D. a homonymous method postEvent is defined, and the method is different from the homonymous method in that an eventName parameter is added to the homonymous method, and the parameter is used for customizing the event name of the global event. The method is generally used for transmitting only one basic data type in the data information of the global event, so that the global event of an event class does not need to be defined separately.
According to the technical scheme provided by the embodiment of the application, functions involved in realizing the global event bus control method are fewer, custom event classes are not required to be added when a global event is newly added, and maintainability of the global event bus is improved. Meanwhile, different proxy mechanisms are provided for different global events in the embodiment of the application, and the proxy mechanisms comprise a receiving mechanism and a sending mechanism, so that the user can operate more conveniently when sending or receiving the global events, and the processing efficiency of the global events is improved.
In some embodiments, listening for the generation of a global event includes:
the common variable is invoked based on the global event to control the global event via the common variable.
In an exemplary embodiment of the present application, a FlowBus class is defined, and the class includes a global common variable application, where the global common variable is used to provide a context of a global event bus corresponding to an entire application, and the application variable is initialized by an init method.
In another exemplary embodiment of the present application, the event class inherited from the viewmodelstoreown is customized to obtain a globally unique ViewModel, and further, the event class inherited from the ViewModel is customized, where the ViewModel is used for generating, storing and transceiving a global event, and the ViewModel may ensure that the global event is not lost in the whole lifecycle of the application.
In the above example, the MutableSeredFlow is used to process the transceiving of the common event and the sticky event, and the life cycle perception can be achieved by matching with the life cycle in the android system. Meanwhile, the transmission and life cycle perception of global events are realized, and the design thought that LiveData has life cycle perception capability and cannot be complied due to the fact that LiveData is required to be rewritten when non-sticky events are processed through LiveDataBus is avoided. Meanwhile, the embodiment of the application provides a method for solving the problems of the data class and the basic data class event which need to customize the event, wherein the custom data class uses class names as event names, the basic data type event customizes the event names, and the custom event names can be independently managed by one class, so that the trouble of use caused by the fact that all events of EventBus need to customize the data class of the event is avoided.
In an exemplary embodiment of the present application, the init method of the FlowBus class is called in the onCreate method included in the custom Application class, and is transferred into the Application. The method comprises the steps of calling an observevert method at a place such as Activity, fragment where a global event bus detects that a global event needs to be received, using a custom event class or an incoming event name according to specific situations, calling a posteventDelay method at a place where a global event bus detects that an event needs to be sent, and using the custom event class or the incoming event name according to specific situations.
In another exemplary embodiment of the present application, after detecting that a user sets a header, a sending function is called to notify the global monitoring of data information update, and data information corresponding to the update is sent to a receiving party through the sending function, so that a page where user information changes can be monitored according to a method of calling a monitoring event to notify the global monitoring of the page to update the user information.
In still another exemplary embodiment of the present application, when it is detected that a user enters commodity details from a commodity list, commodity information is transmitted through a transmission function, and the commodity information is monitored on a commodity details page through a monitoring event method, so that when the user enters the commodity details page, the information of the commodity details can be displayed on a screen of a terminal device at the first time, and detailed commodity information is displayed for the user.
The method according to the above embodiment may be performed by a global event bus constructed according to the structure shown in fig. 3.
It should be understood that the sequence number of each step in the foregoing embodiment does not mean that the execution sequence of each process should be determined by the function and the internal logic, and should not limit the implementation process of the embodiment of the present application.
Any combination of the above optional solutions may be adopted to form an optional embodiment of the present application, which is not described herein.
The following are examples of the apparatus of the present application that may be used to perform the method embodiments of the present application. For details not disclosed in the embodiments of the apparatus of the present application, please refer to the embodiments of the method of the present application.
Fig. 4 is a schematic diagram of a global event bus control device according to an embodiment of the present application. As shown in fig. 4, the global event bus control device includes: the device comprises a monitoring module 401, an acquisition module 402, a determination module 403 and a control module 404.
A listening module 401 configured to listen for global events of an application;
the acquiring module 402 is configured to monitor the generation of the global event, acquire the event name and the classification label of the global event based on the acquiring function in the constructed global event bus, wherein the classification label comprises a sticky label and a non-sticky label, the sticky label represents that the global event is a sticky event, and the non-sticky label represents that the global event is a non-sticky event;
a determining module 403 configured to determine a recipient of the global event and a corresponding lifecycle based on the receive function in the global event bus;
a control module 404 configured to control the receiving party to receive the global event based on the lifecycle and the classification tags.
In some embodiments, the determining module 403 is configured to, after obtaining the event name and the class label of the global event based on the obtaining function in the constructed global event bus, further:
judging whether a global event exists in the corresponding event set or not based on the event name and the classification label;
if the global event does not exist, acquiring data information of the global event, and creating the global event in an event set according to the data information;
if so, invoking a global event in the corresponding event set.
In some embodiments, before the determining module 403 is configured to determine the recipient of the global event and the corresponding lifecycle based on the receive function in the global event bus, it is further configured to:
invoking a data stream of the global event based on the classification tag, and transmitting data information of the global event based on the transmission function;
invoking the data stream of the global event based on the class label, and transmitting the data information of the global event based on the transmission function includes:
when the classification label is a sticky label, transmitting data information of a global event based on a transmission function, a sticky event set and a sticky data stream;
and when the classification label is a non-sticky label, transmitting data information of the global event based on the transmission function, the non-sticky event set and the non-sticky data stream.
In some embodiments, the transmission function further comprises a delay time, the determination module 403 is configured to transmit the data information of the global event based on the transmission function for:
judging whether delay time exists in the global event or not based on the data information;
if the data exists, acquiring a corresponding delay event data class through a global lifecycle management class function based on delay time, and calling a sending function to send data information based on the delay event data class;
and if not, setting the delay time to be zero, and calling a sending function to send data information based on the delay event data class.
In some embodiments, the control module 404 is configured to control the recipient to receive the global event based on the lifecycle and the classification tags for:
controlling a receiving party to receive a global event based on a receiving function, wherein the receiving function comprises a life cycle holder, a minimum life cycle state, a coroutine type where callback event data are processed, an event name, data information and callback data information for the receiving party;
when the current corresponding life cycle of the global event is not greater than the minimum life cycle state, controlling the receiver to stop receiving the global event;
when the fact that the life cycle corresponding to the global event is larger than the minimum life cycle state is detected, controlling a receiving party to receive the global event;
the control receiver receives the global event, further comprising:
when the classification label corresponding to the global event is a sticky label, the receiver is controlled to repeatedly receive the global event according to a preset repetition value;
and when the classification label corresponding to the global event is a non-sticky label, controlling the receiving party not to repeatedly receive the global event.
In some embodiments, the process of building a global event bus includes:
constructing a global class corresponding to the global event bus based on Kotlen, wherein the global class comprises public variables;
the common variable is initialized.
In some embodiments, the snoop module 401 is configured to snoop the generation of global events for:
the common variable is invoked based on the global event to control the global event via the common variable.
Fig. 5 is a schematic diagram of an electronic device 5 according to an embodiment of the present application. As shown in fig. 5, the electronic apparatus 5 of this embodiment includes: a processor 501, a memory 502 and a computer program 503 stored in the memory 502 and executable on the processor 501. The steps of the various method embodiments described above are implemented by processor 501 when executing computer program 503. Alternatively, the processor 501, when executing the computer program 503, performs the functions of the modules/units in the above-described apparatus embodiments.
The electronic device 5 may be a desktop computer, a notebook computer, a palm computer, a cloud server, or the like. The electronic device 5 may include, but is not limited to, a processor 501 and a memory 502. It will be appreciated by those skilled in the art that fig. 5 is merely an example of the electronic device 5 and is not limiting of the electronic device 5 and may include more or fewer components than shown, or different components.
The processor 501 may be a central processing unit (Central Processing Unit, CPU) or other general purpose processor, digital signal processor (Digital Signal Processor, DSP), application specific integrated circuit (Application Specific Integrated Circuit, ASIC), field programmable gate array (Field-Programmable Gate Array, FPGA) or other programmable logic device, discrete gate or transistor logic device, discrete hardware components, or the like.
The memory 502 may be an internal storage unit of the electronic device 5, for example, a hard disk or a memory of the electronic device 5. The memory 502 may also be an external storage device of the electronic device 5, for example, a plug-in hard disk, a Smart Media Card (SMC), a Secure Digital (SD) Card, a Flash Card (Flash Card) or the like, which are provided on the electronic device 5. Memory 502 may also include both internal storage units and external storage devices of electronic device 5. The memory 502 is used to store computer programs and other programs and data required by the electronic device.
It will be apparent to those skilled in the art that, for convenience and brevity of description, only the above-described division of the functional units and modules is illustrated, and in practical application, the above-described functional distribution may be performed by different functional units and modules according to needs, i.e. the internal structure of the apparatus is divided into different functional units or modules to perform all or part of the above-described functions. The functional units and modules in the embodiment may be integrated in one processing unit, or each unit may exist alone physically, or two or more units may be integrated in one unit, where the integrated units may be implemented in a form of hardware or a form of a software functional unit.
The integrated modules/units may be stored in a readable storage medium if implemented in the form of software functional units and sold or used as stand-alone products. Based on such understanding, the present application may implement all or part of the flow of the method of the above embodiment, or may be implemented by a computer program to instruct related hardware, and the computer program may be stored in a readable storage medium, where the computer program may implement the steps of the method embodiments described above when executed by a processor. The computer program may comprise computer program code, which may be in source code form, object code form, executable file or in some intermediate form, etc. The computer readable medium may include: any entity or device capable of carrying computer program code, a recording medium, a U disk, a removable hard disk, a magnetic disk, an optical disk, a computer Memory, a Read-Only Memory (ROM), a random access Memory (Random Access Memory, RAM), an electrical carrier signal, a telecommunications signal, a software distribution medium, and so forth. It should be noted that the content of the computer readable medium can be appropriately increased or decreased according to the requirements of the jurisdiction's jurisdiction and the patent practice, for example, in some jurisdictions, the computer readable medium does not include electrical carrier signals and telecommunication signals according to the jurisdiction and the patent practice.
The above embodiments are only for illustrating the technical solution of the present application, and are not limiting; although the application 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 scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present application, and are intended to be included in the scope of the present application.

Claims (10)

1. A global event bus control method, comprising:
monitoring global events of the application program;
monitoring the generation of the global event, and acquiring an event name and a classification label of the global event based on an acquisition function in a constructed global event bus, wherein the classification label comprises a sticky label and a non-sticky label, the sticky label represents that the global event is a sticky event, and the non-sticky label represents that the global event is a non-sticky event;
determining a receiver of the global event and a corresponding life cycle based on a receiving function in the global event bus;
the receiver is controlled to receive the global event based on the lifecycle and the classification tag.
2. The method of claim 1, further comprising, after obtaining the event name and class label of the global event based on the obtaining function in the constructed global event bus:
judging whether the global event exists in a corresponding event set or not based on the event name and the classification label, wherein the event set comprises a sticky event set and a non-sticky event set, the sticky event set is used for storing all sticky events, and the non-sticky event set is used for storing all non-sticky events;
if the global event does not exist, acquiring data information of the global event, and creating the global event in the event set according to the data information;
and if so, calling the global event in the corresponding event set.
3. The method of claim 1, wherein prior to determining the recipient of the global event and the corresponding lifecycle based on a receive function in the global event bus, further comprising:
invoking a data stream of the global event based on the classification tag, and transmitting data information of the global event based on a transmission function;
invoking the data flow of the global event based on the classification tag, and transmitting the data information of the global event based on a transmission function comprises:
when the classification tag is a sticky tag, transmitting data information of the global event based on the transmission function, the sticky event set and a sticky data stream;
and when the classification label is a non-sticky label, transmitting data information of the global event based on the transmitting function, the non-sticky event set and a non-sticky data stream.
4. The method of claim 3, wherein the transmit function further comprises a delay time, and wherein transmitting the data information for the global event based on the transmit function comprises:
judging whether delay time exists in the global event or not based on the data information;
if the data information exists, acquiring a corresponding delay event data class through a global lifecycle management class function based on the delay time, and calling the sending function to send the data information based on the delay event data class;
and if not, setting the delay time to be none, and calling the sending function to send the data information based on the delay event data class.
5. The method of claim 1, wherein controlling the recipient to receive the global event based on the lifecycle and the classification tag comprises:
controlling the receiving party to receive the global event based on the receiving function, wherein the receiving function comprises a life cycle holder, a minimum life cycle state, a coroutine type where callback event data are processed, an event name, data information and callback of the data information for the receiving party;
when the life cycle corresponding to the global event is detected to be not greater than the minimum life cycle state, controlling the receiver to stop receiving the global event;
when the life cycle corresponding to the global event is detected to be greater than the minimum life cycle state, controlling the receiving party to receive the global event;
controlling the receiver to receive the global event, further comprising:
when the classification label corresponding to the global event is an adhesive label, the receiver is controlled to repeatedly receive the global event according to a preset repetition value;
and when the classification label corresponding to the global event is a non-sticky label, controlling the receiving party not to repeatedly receive the global event.
6. The method according to any one of claims 1 to 5, wherein the process of building the global event bus comprises:
constructing a global class corresponding to the global event bus based on Kotlen, wherein the global class comprises public variables;
initializing the common variable.
7. The method of claim 6, wherein monitoring for the generation of the global event comprises:
invoking the common variable based on the global event to control the global event through the common variable.
8. A global event bus control device, comprising:
the monitoring module is configured to monitor global events of the application program;
the acquisition module is configured to monitor the generation of the global event, acquire an event name and a classification label of the global event based on an acquisition function in a constructed global event bus, wherein the classification label comprises a sticky label and a non-sticky label, the sticky label represents that the global event is a sticky event, and the non-sticky label represents that the global event is a non-sticky event;
a determining module configured to determine a recipient of the global event and a corresponding lifecycle based on a receive function in the global event bus;
a control module configured to control the recipient to receive the global event based on the lifecycle and the classification tag.
9. An electronic device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that the processor implements the steps of the method according to any of claims 1 to 7 when the computer program is executed.
10. A readable storage medium storing a computer program, characterized in that the computer program when executed by a processor implements the steps of the method according to any one of claims 1 to 7.
CN202310965048.5A 2023-07-31 2023-07-31 Global event bus control method and device, electronic equipment and readable storage medium Pending CN116974723A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310965048.5A CN116974723A (en) 2023-07-31 2023-07-31 Global event bus control method and device, electronic equipment and readable storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310965048.5A CN116974723A (en) 2023-07-31 2023-07-31 Global event bus control method and device, electronic equipment and readable storage medium

Publications (1)

Publication Number Publication Date
CN116974723A true CN116974723A (en) 2023-10-31

Family

ID=88484662

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310965048.5A Pending CN116974723A (en) 2023-07-31 2023-07-31 Global event bus control method and device, electronic equipment and readable storage medium

Country Status (1)

Country Link
CN (1) CN116974723A (en)

Similar Documents

Publication Publication Date Title
KR20110076954A (en) Optimized polling in low resource devices
EP3547715A1 (en) Method and apparatus for reducing continuous-wakeup delay of bluetooth loudspeaker, and bluetooth loudspeaker
WO2021151312A1 (en) Method for determining inter-service dependency, and related apparatus
CN112416632B (en) Event communication method and device, electronic equipment and computer readable medium
CN116627333A (en) Log caching method and device, electronic equipment and computer readable storage medium
CN109788251B (en) Video processing method, device and storage medium
CN110912807A (en) Information prompting method and device, electronic equipment and computer readable storage medium
CN108764866B (en) Method and equipment for allocating resources and drawing resources
CN112965799B (en) Task state prompting method and device, electronic equipment and medium
CN112084042A (en) Message processing method and device
CN116880990A (en) Task execution method, device, electronic equipment and computer readable storage medium
CN113127225A (en) Method, device and system for scheduling data processing tasks
CN116974723A (en) Global event bus control method and device, electronic equipment and readable storage medium
US11277300B2 (en) Method and apparatus for outputting information
CN111460020B (en) Method, device, electronic equipment and medium for resolving message
CN111290873B (en) Fault processing method and device
CN113220342A (en) Centralized configuration method and device, electronic equipment and storage medium
CN112463616A (en) Chaos testing method and device for Kubernetes container platform
CN109614137B (en) Software version control method, device, equipment and medium
CN112579447A (en) Browser testing method and device
CN112671822B (en) Service request processing method, device, storage medium, server and system
CN113766437B (en) Short message sending method and device
CN110262756B (en) Method and device for caching data
CN114979187B (en) Data processing method and device
CN111784380B (en) Advertisement putting attribution method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
TA01 Transfer of patent application right

Effective date of registration: 20240118

Address after: No. 13 Xingxiang Road, Zengjia Town, High tech Zone, Shapingba District, Chongqing, 400039

Applicant after: Chongqing Selis Phoenix Intelligent Innovation Technology Co.,Ltd.

Address before: 401120 No. 618 Liangjiang Avenue, Longxing Town, Yubei District, Chongqing City

Applicant before: Chongqing Celes New Energy Automobile Design Institute Co.,Ltd.

TA01 Transfer of patent application right