Data storage method, device, electronic device and storage medium
By replacing the Android Sharedpreferences component with a new storage component that includes both native and large data storage capabilities and dynamically switching storage methods, the method addresses inefficiencies in large data storage, ensuring reliable data handling and preventing system crashes while enhancing user experience.
Patent Information
- Application Number
- CN202111531255.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-14
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2041-12-14
AI Technical Summary
The Sharedpreferences component of Android native system has poor performance when stored in big data, which may lead to data loss or system crash.
When the application starts, replace the native SharedPreferences component with a new SharedPreferences component that includes native storage methods and big data storage methods. Select a suitable storage unit for storage according to the amount of data to be stored, store big data in the form of files and/or databases, and retain the storage advantages of native SharedPreferences.
It realizes reliable storage of big data, avoids system crashes, and improves the overall storage performance and user experience of electronic devices.
Smart Images

Figure CN114185746B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of data processing, and particularly relates to a data storage method, apparatus, electronic device, and storage medium. Background Art
[0002] The SharedPreferences (SP) component of the Android native system is used for local storage of key-value pairs, which are usually stored in the form of XML (Extensible Markup Language) files. The storage is convenient and the access rate is fast. However, the SharedPreferences component is usually suitable for the storage of lightweight data, and its storage performance is poor for large data. Especially when the amount of data stored at one time is too large, data loss may occur or the system may crash. Summary of the Invention
[0003] Embodiments of the present disclosure provide a data storage method, apparatus, electronic device, and storage medium, which can solve the technical problem of poor performance when using the SharedPreferences component for large data storage in the prior art.
[0004] According to one of the solutions of the present disclosure, a data storage method is provided, including:
[0005] Obtaining data to be stored during the operation of an application;
[0006] Judging whether the amount of the data to be stored is greater than a preset data amount threshold;
[0007] If so, storing the data to be stored using a first storage unit in a first storage component; if not, storing the data to be stored using a second storage unit in the first storage component, where the first storage component is obtained by replacing a second storage component when the application starts, and the storage method of the second storage component is the same as that of the second storage unit.
[0008] In some embodiments, when the application starts, replacing the second storage component with the first storage component includes:
[0009] When the application starts, obtaining an instance of the application's application object;
[0010] Obtaining the second storage component of the application through the reflection mechanism based on the instance of the application object;
[0011] Replacing the second storage component with the first storage component.
[0012] In some embodiments, obtaining a second storage component of the application based on the application object instance through a reflection mechanism includes:
[0013] Obtaining the package name of the application based on the application object instance;
[0014] Obtaining the second storage component corresponding to the application from a preset storage variable based on the package name of the application, wherein the preset storage variable stores the package names of multiple applications and the corresponding second storage components in a map structure.
[0015] In some embodiments, replacing the second storage component with the first storage component includes:
[0016] Determining the second storage instance of the second storage component as the second storage unit;
[0017] Adding the first storage unit including the first storage instance to the second storage component to form the first storage component.
[0018] In some embodiments, the method further includes:
[0019] Creating a custom first storage instance based on the application;
[0020] At the compilation stage of the application, encapsulating the first storage instance to obtain the first storage unit.
[0021] In some embodiments, the method further includes:
[0022] At the compilation stage of the application, encapsulating the first storage instance and the second storage instance corresponding to the second storage component to obtain the first storage component;
[0023] When the application is started, replacing the second storage component with the first storage component.
[0024] In some embodiments, the first storage component is a SharedPreferences component, the first storage unit stores the data to be stored in the form of a file and / or a database, and the second storage unit stores the data to be stored in the form of xml.
[0025] According to one of the solutions of the present disclosure, there is also provided a data storage device, including:
[0026] An obtaining module configured to obtain data to be stored during the operation of the application;
[0027] A judgment module, configured to judge whether the data volume of the data to be stored is greater than a preset data volume threshold;
[0028] A storage module, configured to, if the data volume of the data to be stored is greater than the preset data volume threshold, store the data to be stored using a first storage unit in a first storage component; if the data volume of the data to be stored is not greater than the preset data volume threshold, store the data to be stored using a second storage unit in the first storage component, where the first storage component is obtained by replacing a second storage component when the application program is started, and the storage method of the second storage component is the same as the storage method of the second storage unit.
[0029] According to one of the solutions of the present disclosure, there is also provided an electronic device, including a processor and a memory; the memory stores computer execution instructions; when the processor executes the computer execution instructions stored in the memory, the data storage method described above is implemented.
[0030] According to one of the solutions of the present disclosure, there is also provided a computer-readable storage medium, on which computer program instructions are stored, and when the computer program instructions are executed by a processor, the data storage method described above is implemented.
[0031] The data storage method, device, electronic device and storage medium provided by various embodiments of the present disclosure can, by replacing the original second storage component with a first storage component that includes both the original storage method and a new big data storage method when the application program is started, select a corresponding storage unit in the first storage component to store the data to be stored in real time according to the data volume of the data to be stored during the running of the application program, can achieve reliable storage of different data, and can avoid system crashes caused by excessive data volume, ensure the smooth running of the application program, improve the overall storage performance of the electronic device, and improve the user experience. Description of the Drawings
[0032] Figure 1 A flowchart showing the data storage method according to an embodiment of the present disclosure;
[0033] Figure 2 Another flowchart showing the data storage method according to an embodiment of the present disclosure;
[0034] Figure 3 Another flowchart showing the data storage method according to an embodiment of the present disclosure;
[0035] Figure 4 Another flowchart showing the data storage method according to an embodiment of the present disclosure;
[0036] Figure 5 A schematic structural diagram showing the data storage device according to an embodiment of the present disclosure;
[0037] Figure 6 Schematic structural diagram of an electronic device showing an embodiment of the present disclosure. Detailed implementation manners
[0038] Reference is made herein to the various solutions and features of the present disclosure with reference to the accompanying drawings.
[0039] It should be understood that various modifications can be made to the embodiments applied herein. Therefore, the above description should not be regarded as a limitation, but only as an example of the embodiments. Those skilled in the art will think of other modifications within the scope and spirit of the present disclosure.
[0040] The accompanying drawings, which are included in and constitute a part of this specification, illustrate embodiments of the present disclosure and, together with the general description of the present disclosure given above and the detailed description of the embodiments given below, serve to explain the principles of the present disclosure.
[0041] These and other features of the present disclosure will become apparent from the following description of the preferred forms of the embodiments given as non - limiting examples with reference to the accompanying drawings.
[0042] It should also be understood that although the present disclosure has been described with reference to some specific examples, those skilled in the art can surely implement many other equivalent forms of the present disclosure.
[0043] When combined with the accompanying drawings, the above and other aspects, features, and advantages of the present disclosure will become more apparent in view of the following detailed description.
[0044] Hereinafter, specific embodiments of the present disclosure are described with reference to the accompanying drawings; however, it should be understood that the embodiments applied are only examples of the present disclosure and can be implemented in various ways. Well - known and / or repetitive functions and structures are not described in detail to avoid obscuring the present disclosure with unnecessary or redundant details. Therefore, the specific structural and functional details applied herein are not intended to be limiting, but only as a basis and representative basis for the claims to teach those skilled in the art to use the present disclosure in substantially any suitable detailed structure in a variety of ways.
[0045] The data storage method provided by the embodiments of the present disclosure is applied to an electronic device with an operating system. The electronic device may include various handheld devices with wireless communication functions (such as mobile phones, tablet computers, PDAs, etc.), vehicle-mounted devices, wearable devices (such as smart watches, smart bracelets, pedometers, etc.), computing devices or other processing devices connected to a wireless modem, as well as various forms of user equipment (UE), mobile stations (MS), terminal devices, etc. The electronic device has different operating systems to provide corresponding operating environments for different applications. For example, the mobile phone operating system can be the Android system or the iOS system.
[0046] In the embodiments of the present disclosure, the data storage method is described in detail by taking an Android device installed with the Android system as an example.
[0047] Figure 1 The flowchart of the data storage method provided by the embodiments of the present disclosure is shown. As Figure 1 shown, the embodiments of the present disclosure provide a data storage method, including the following steps:
[0048] S101: Obtain the data to be stored during the running of the application.
[0049] Among them, the data to be stored refers to the relevant data generated or used by the application (APP) from startup to shutdown and exit during the entire running process. During the startup and running of the application, it needs to go through many processes, such as program reading, page loading, calculation, input and output, etc. A lot of data will be generated during these processes, and they can be pre-stored in the memory for the application to read and call during running; at the same time, after the application starts, the user will perform various operations. When the user starts the application next time, they may hope to maintain the previous operation state. Therefore, some data needs to be saved for calling when the application is running or closed.
[0050] The data to be stored includes but is not limited to: configuration information, log information, cache data, etc. Exemplarily, the configuration information can be the configuration information generated by different pages during the running of the application. The Android system can monitor the running of the application in real time, and after the current page is loaded, the configuration information of this page can be determined as a piece of data to be stored. When another page is loaded, the configuration information of another page can be determined as another piece of data to be stored, that is, different data to be stored that need to be stored at a single time are determined according to the loading of different pages.
[0051] In specific implementation, other methods can also be used to determine the data to be stored. For example, by setting a timer, the configuration information generated during a certain time interval of the application running can be determined as the data to be stored; for another example, the data to be stored can be determined according to the user's selection. When the user needs to save the current page, after the user selects the current page, the page data corresponding to the current page can be determined as the data to be stored.
[0052] The data type of the data to be stored can be common data types such as int, long, boolean, String, Float, Set, and Map, or any other arbitrary data type, which is not specifically limited in this disclosure.
[0053] S102: Determine whether the data volume of the data to be stored is greater than a preset data volume threshold.
[0054] After obtaining the data to be stored that needs to be stored once, the data volume of the data to be stored can be detected to determine whether the data volume of the data to be stored is greater than a preset data volume threshold, so as to perform corresponding storage according to the data volume size of the data to be stored.
[0055] S103: If so, use the first storage unit in the first storage component to store the data to be stored; if not, use the second storage unit in the first storage component to store the data to be stored, where the first storage component is obtained by replacing the second storage component when the application starts, and the storage method of the second storage component is the same as that of the second storage unit.
[0056] Among them, the second storage component is the storage component originally in the Android system for storing lightweight data; the first storage component is the storage component after replacing the second storage component. The replaced first storage component includes a first storage unit and a second storage unit. The first storage unit has good big data storage performance and can achieve fast storage of big data; the second storage unit retains the storage method of the original second storage component of the system and can achieve fast storage of lightweight data.
[0057] Since the second storage component of the Android system natively is usually used to store lightweight data, when the amount of data to be stored is large, using the second storage component for storage may cause some data to fail to be effectively stored, resulting in data loss; or the system may crash due to the excessive amount of data stored at one time. To ensure the effective storage of data and prevent the system from crashing, in this step, when the amount of the data to be stored is greater than the preset data volume threshold, the first storage unit is used to store the data to be stored; when the amount of the data to be stored is less than or equal to the preset data volume threshold, the second storage unit with the same storage method as the second storage component is used to store the data to be stored. This can not only effectively store the data with a large amount, prevent data loss, and prevent the system from crashing, ensuring the smooth operation of the application program, but also utilize the native second storage component of the system to quickly and conveniently store lightweight data, improving the overall storage performance of the electronic device.
[0058] In this embodiment, when the application program is started, the first storage component is used to replace the second storage component. During the subsequent operation of the application program, as long as the data to be stored and its data volume are determined, the corresponding storage unit can be used for data storage; at the same time, since the first storage component is directly used to replace the second storage component, therefore, by calling the application programming interface (Application Programming Interface, API) corresponding to the native second storage component of the Android system, the storage unit corresponding to the first storage component can be called to store the data to be stored, that is, in this embodiment, the API corresponding to the native second storage component of the Android system is used for data storage without the need to additionally set the corresponding API, which can greatly improve the data storage efficiency, and the entire storage process still uses the original system API for calling, making it difficult for users to perceive and not affecting the implementation of other logics, which is convenient to operate and can improve the user experience. In addition, after calling the first storage component through the same API, the system can select the corresponding storage unit according to the data volume of the data to be stored to store different data to be stored, which can effectively avoid the miscall of the API and ensure the smooth and reliable storage of data.
[0059] The data storage method provided by the embodiments of the present disclosure can replace the native second storage component with the first storage component that includes both the native storage method and the new large data storage method when the application program is started. During the operation of the application program, the corresponding storage unit in the first storage component can be selected in real time according to the data volume of the data to be stored to store the data to be stored, which can realize the reliable storage of different data, avoid the system crash caused by excessive data volume, ensure the smooth operation of the application program, improve the overall storage performance of the electronic device, and improve the user experience.
[0060] Optionally, the first storage component is a SharedPreferences component. The first storage unit stores the data to be stored in the form of a file and / or a database, and the second storage unit stores the data to be stored in the form of xml. That is, the second storage component is the native SharedPreferences component of the Android system. In this embodiment, a new first SharedPreferences component is used to replace the native second SharedPreferences component of the Android system.
[0061] The Android system provides a SharedPreferences class through the native second SharedPreferences component to store some lightweight data of the application. For example, the native second SharedPreferences component can store the state of the application's Activity. When the Activity pauses, the state of the Activity is saved to the SharedPereferences. When the Activity is reloaded and the system callback method onSaveInstanceState is called, the corresponding value is retrieved from the SharedPreferences again.
[0062] The second SharedPreferences component generates an xml file through the Android system and writes the data in the form of key-value (key-value pair) in this xml file for storage. The key represents the keyword of the stored data in the SP.xml file, and the value represents the stored value of the stored data in the SP.xml.
[0063] In the Android system, the data is basically private and is usually stored in the directory of "data / data / package name". Therefore, the xml file stored using the second SharedPreferences component is usually saved in the directory of " / data / data / package name / shared_prefs". The application creates an SP.xml file each time it stores data, and an application can create multiple xml files.
[0064] The second storage unit stores the same storage logic as the native second SharedPreferences component of the Android system. When it is determined that the amount of data to be stored is less than or equal to the preset data volume threshold, the second storage unit is used for storage. Since the original second SharedPreferences component eliminates many operations such as creating a database, creating a table, and writing SQL statements compared to a database, the storage operation is more convenient and concise. In addition, after the data is first obtained using the SharedPreferences component, the data is usually loaded into a cached Map, and subsequent reads will be very fast. Therefore, after using the SharedPreferences component to store data, the subsequent data reading efficiency can be improved.
[0065] The first storage unit stores corresponding file and / or database storage logic. For example, when the first storage unit stores data in the form of a database, its storage logic includes: (1) creating a database; (2) creating a data table and inserting data; (3) modifying data; (4) querying data; (5) deleting data, etc. When the first storage unit stores data in the form of a database, the data to be stored can be stored in the database folder in the format of a data table, and its suffix name can be.db. For example, the storage path of the database can be: / data / data / <packagename> / Share_Prefs_Cashe / databases. In some other embodiments, the data to be stored can also be stored in the database folder in other data file formats. For example, the file name suffix can also include any format such as abc.xml, abc.json, abc.png, abc.cache, etc.
[0066] When the first storage unit stores data in the form of a file, the data to be stored can be directly stored in the directory in the form of a file. For example, the storage path of the file can be: / data / data / <packagename> / Share_Prefs_Cashe / files。
[0067] It can be understood that when the amount of data to be stored is greater than the preset data volume threshold, the data to be stored can be stored separately in the form of a file or a database, or both a file storage logic and a database storage logic can be set in the first storage unit, so that the user can select the corresponding storage method according to actual needs, further improving the convenience of big data storage.
[0068] In this embodiment, the preset data volume threshold can be determined according to the storage limit of the second storage component. For example, in this embodiment, the second SharedPreferences component is usually used to store lightweight data such as configuration parameters of an application. Therefore, the preset data volume threshold can be set to be relatively small. For example, the preset data volume threshold is 1M.
[0069] In this embodiment, the ability of the system's native SharedPreferences to store data in xml format is retained, and the ability to store data in file and / or database format is added. During the operation of the application, the storage method is dynamically switched according to the amount of data to be stored. On the basis of ensuring the original advantages of the SharedPreferences component such as convenient storage and fast access speed, effective and reliable storage of big data can be achieved, improving the overall storage and operation performance of the electronic device.
[0070] In some other embodiments, the second storage component can also store the data to be stored in the form of a database, such as a lightweight (or small) database. Exemplary lightweight databases can be Access, MSDE, Embedded Firebird, SQLite, MySQL, etc., which have better storage performance (including storage rate, throughput, latency sensitivity, etc.) for lightweight data. The first storage unit stores data in the form of a medium or large database, such as Sql Server.
[0071] It can be understood that the first storage unit having good big data storage performance not only means that the first storage unit has a large storage space, but also means that the first storage unit has a better storage rate, etc. For example, it is also possible to store some big data in xml format, but there is a problem of slow data storage rate.
[0072] In this embodiment, the data stored in the first storage unit and the second storage unit are both stored in the internal storage space. Internal storage is generally obtained and operated using Context. Therefore, in this embodiment, the SharedPreferences instances corresponding to different storage units of the first storage component can be obtained through the getSharedPreferences method provided by Context. Among them, the SharedPreferences instance is an instantiated SharedPreferences component, which contains the corresponding SharedPreferences storage logic. Exemplarily, the corresponding SharePreferences instance can be obtained by calling the Context.getSharedPreferences(package name, SharedPreferences instance) method.
[0073] In some embodiments, as Figure 2 shown, when the application is started, the second storage component is replaced with the first storage component, which specifically includes the following steps:
[0074] S201: When the application is started, obtain the application object instance of the application;
[0075] S202: Based on the application object instance, obtain the second storage component of the application through the reflection mechanism;
[0076] S203: Replace the second storage component with the first storage component.
[0077] Specifically, when the application is started, the application object instance of the application is obtained by calling the system API of the Android system. Each application has an application class. When the application is started, the Android system will trigger the methods of the application class in the application. The system will automatically create an Application object and instantiate this Application object to store some information of the system. There is only one application object during the running process of the entire application, that is, all users accessing this application share one application object. In actual development, developers can add the required logic to this application class. For example, add the initialization logic of some services to initialize the instance of this application class.
[0078] The electronic device can obtain the application object instance (application variable) of the application program by calling the system API, and then find the corresponding second storage component of the application program (i.e., find the native storage component of the Android system) through the reflection mechanism based on the obtained application object instance, and then use the first storage component to replace the second storage component.
[0079] In some embodiments, in step S202, obtaining the second storage component of the application program through the reflection mechanism based on the application object instance includes:
[0080] S2021: Obtain the package name of the application program based on the application object instance;
[0081] S2022: Obtain the second storage component corresponding to the application program from the preset storage variable, where the preset storage variable stores the package names and corresponding second storage components of multiple application programs in a map structure.
[0082] In this embodiment, the second storage component is the second SharedPreferences component. Therefore, the preset storage variable can be the sSharePrefsCashe variable. The sSharePrefsCashe variable stores the package names and corresponding SharedPreferences instances of multiple different application programs in a map storage structure (key-value, key-value pair), where the key represents the package name of the application program and the value represents the SharedPreferences instance of the application program. The preset storage variable can be stored in the electronic device or in an external server (such as a cloud server).
[0083] Optionally, the reflection mechanism is the Java reflection mechanism. The Java reflection mechanism is a function for dynamically obtaining information and dynamically calling the methods of an object, that is, at runtime of the application program, classes or objects are dynamically loaded and detailed information about the classes or objects is obtained, so as to operate (such as call) the attributes and methods of the classes or objects. Among them, a class is the organizational unit of a Java program, and any Java statement must be written in a class file. In specific implementation, other reflection mechanisms can also be used to obtain variables, and the present disclosure does not specifically limit this.
[0084] After the application starts and the application object instance corresponding to the application is obtained, the source code of the application object instance can be analyzed, and the sSharePrefsCashe variable can be obtained by using the Java reflection mechanism. Then, according to the package name of the currently running application, the corresponding second SharedPreferences component (SharedPreferences instance) can be found from the sSharePrefsCashe variable. In a specific implementation, the SharedPreferences instance (value) corresponding to the package name (key) of the application can be obtained through the getXXX() method.
[0085] In some embodiments, in step S203, replacing the second storage component with the first storage component includes:
[0086] S2031: Determining the second storage instance of the second storage component as the second storage unit;
[0087] S2032: Adding the first storage unit including the first storage instance to the second storage component to form the first storage component.
[0088] Specifically, the SharedPreferences instance corresponding to the original second SharedPreferences component of the Android system is reserved as the second storage unit, and the first storage instance including file and / or database storage logic is added to the original second SharedPreferences component, thereby forming the first SharedPreferences component. In this way, when using the first SharedPreferences component to replace the second SharedPreferences component, only additional files and / or database storage logic need to be added according to the application, and the first storage instance corresponding to the storage logic is added to the position where the original SharedPreferences instance is located. Other configurations in the original first SharedPreferences component do not change, which can achieve fast replacement and is not easily noticed by users.
[0089] In some embodiments, as Figure 3 shown, the method further includes:
[0090] S301: Creating a custom first storage instance based on the application;
[0091] S302: Encapsulating the first storage instance during the compilation phase of the application to obtain the first storage unit.
[0092] Specifically, a custom first storage instance is created based on the written application code during the coding stage of the application. Then, during the compilation stage of the application, the first storage instance is encapsulated to obtain a first storage unit, which is an executable file. When the application starts, the executable file is added to the corresponding position of the second storage component of the application, thus forming a first storage component.
[0093] During the running process of the application, when it is detected that the data volume of the data to be stored is greater than a preset data volume threshold, the system calls the API corresponding to the original second storage component, and will automatically call the first storage unit for data storage; when it is detected that the data volume of the data to be stored is less than or equal to the preset data volume threshold, the system calls the API corresponding to the original second storage component, and will automatically call the second storage unit for data storage. In this way, dynamic switching of the data storage method for the data to be stored is achieved during the running process of the application.
[0094] In some embodiments, as Figure 4 shown, the method further includes:
[0095] S401: During the compilation stage of the application, encapsulate the first storage instance and the second storage instance corresponding to the second storage component to obtain the first storage component;
[0096] S402: When the application starts, use the first storage component to replace the second storage component.
[0097] In the above steps S2031 and S2032, the first storage unit is pre-encapsulated during the compilation stage of the application, and then the first storage unit is added to the position where the second storage unit is located after the application starts, forming a first storage component. In steps S401 to S402, the first storage instance of the first storage unit and the second storage instance corresponding to the second storage component can be pre-configured and encapsulated together during the compilation stage of the application to obtain the first storage component; then, after the application starts, the corresponding second storage component is obtained through the reflection mechanism, and after determining the position of the second storage component, the pre-encapsulated first storage component can be directly used to replace the second storage component. Directly adding the first storage unit to the second storage component in steps S2031 and S2032 can reduce the data processing volume and improve the processing efficiency; while steps S401 and S402 can achieve fast replacement and improve the running efficiency of the application after startup by pre-encapsulating to form the first storage component.
[0098] In the above embodiments, the data storage method of the embodiments of the present disclosure is described based on the SharedPreferences storage of the Android system. In specific implementation, the above method can also be applied to the data storage of application programs running on other operating systems. For example, the iOS system provides the NSUserDefaults class to store the basic configuration information of some users with a small amount of data. To prevent data loss or system crashes that may occur when storing basic configuration information exceeding a certain data volume threshold, the NSUserDefaults class provided by the iOS system can be used as the second storage component as in the above method, and a first storage component that includes both the native NSUserDefaults storage method and a new large data storage method can be custom-created. When the application program starts, the first storage component is used to replace the second storage component, so that during the operation of the application program on the iOS system, different storage methods are dynamically switched according to the data volume of the data to be stored for data storage, ensuring the storage performance of the native NSUserDefaults component while providing better large data storage performance.
[0099] It can be understood that the above embodiments mainly describe the data storage method. When it is necessary to read the stored data stored using the above data storage method from the first storage unit or the second storage unit, the required data can be retrieved from the storage unit corresponding to the first storage component by calling the API corresponding to the original second storage component according to the data volume of the data to be read. The specific data reading method can refer to the above data storage method and will not be elaborated here.
[0100] Figure 5 It is a schematic structural diagram of the data storage device of the embodiments of the present disclosure. As Figure 5 shown, the embodiments of the present disclosure provide a data storage device, including:
[0101] An acquisition module 501, configured to acquire data to be stored during the operation of the application program;
[0102] A judgment module 502, configured to judge whether the data volume of the data to be stored is greater than a preset data volume threshold;
[0103] A storage module 503, configured to, if the data volume of the data to be stored is greater than the preset data volume threshold, store the data to be stored using the first storage unit in the first storage component; if the data volume of the data to be stored is not greater than the preset data volume threshold, store the data to be stored using the second storage unit in the first storage component, where the first storage component is obtained by replacing the second storage component when the application program starts, and the storage method of the second storage component is the same as the storage method of the second storage unit.
[0104] Optionally, the first storage component is a SharedPreferences component. The first storage unit stores the data to be stored in the form of a file and / or a database, and the second storage unit stores the data to be stored in the form of xml.
[0105] In some embodiments, the data storage device further includes a replacement module configured to replace the second storage component with the first storage component when the application is started. The replacement module specifically includes:
[0106] A first obtaining unit configured to obtain an instance of the application's application object when the application is started;
[0107] A second obtaining unit configured to obtain the second storage component of the application through a reflection mechanism based on the application object instance;
[0108] A replacement unit configured to replace the second storage component with the first storage component.
[0109] In some embodiments, the second obtaining unit is specifically configured to:
[0110] Obtain the package name of the application based on the application object instance;
[0111] Obtain the second storage component corresponding to the application from a preset storage variable, where the preset storage variable stores the package names and corresponding second storage components of multiple applications in a map structure.
[0112] In some embodiments, the replacement unit is specifically configured to:
[0113] Determine the second storage instance of the second storage component as the second storage unit;
[0114] Add the first storage unit including the first storage instance to the second storage component to form the first storage component.
[0115] In some embodiments, the replacement module further includes:
[0116] A creation unit configured to create a custom first storage instance based on the application;
[0117] A first encapsulation unit configured to encapsulate the first storage instance during the compilation phase of the application to obtain the first storage unit.
[0118] In some embodiments, the replacement module further includes:
[0119] A second encapsulation unit, configured to encapsulate the first storage instance and the second storage instance corresponding to the second storage component during the compilation stage of the application program, so as to obtain the first storage component;
[0120] The replacement unit is configured to replace the second storage component with the first storage component encapsulated by the second encapsulation unit when the application program is started.
[0121] The data storage device provided in the embodiments of the present disclosure corresponds to the data storage method in the above embodiments. Based on the above data storage method, those skilled in the art can understand the specific implementation manners and various variations of the data storage device in the embodiments of the present disclosure. Any optional items in the embodiments of the data storage method are also applicable to the data storage device, and will not be elaborated herein.
[0122] Figure 6 It is a schematic structural diagram of an electronic device according to an embodiment of the present disclosure. As Figure 6 shown, the electronic device according to the embodiment of the present disclosure includes a memory 601 and a processor 602. The memory 601 is used to store at least one computer program instruction, and the processor 602 is configured to implement the data storage method as described in the above embodiments by loading and executing the at least one computer program instruction.
[0123] The memory 601 can be used to store software programs and modules, such as program instructions / modules corresponding to the data storage method in the embodiments of the present disclosure, and also such as the application program and the corresponding data to be stored described in the above embodiments. The processor 602 executes various functional applications and data processing by running the software programs and modules stored in the memory 601, that is, implements the above data storage method.
[0124] The memory 601 may include a random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memories, or other non-volatile solid-state memories.
[0125] In some embodiments, the memory 601 further includes a first storage area 6011 and a second storage area 6012. The first storage area 6011 and the second storage area 6012 may be dedicated non-volatile memories. The first storage area 6011 is used to store the files and / or database format data stored by the first storage unit as described above, and the second storage area 6012 is used to store the xml format data stored by the second storage unit as described above. By using the dedicated first storage area 6011 and second storage area 6012 to store the data to be stored with different data volume sizes respectively, it is convenient to read the data in time, improve the data access rate, and ensure the security of data storage.
[0126] An embodiment of the present disclosure also provides a computer storage medium, on which computer program instructions are stored; when the computer program instructions are executed by a processor, the data storage method described in the above embodiment is implemented.
[0127] The computer-readable storage medium of the embodiments of the present disclosure may adopt any combination of one or more computer-readable media. The computer-readable medium may be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples (non-exhaustive list) of the computer-readable storage medium include: an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the embodiments of the present disclosure, the computer-readable storage medium may be any tangible medium that contains or stores a program, and the program may be used or combined with an instruction execution system, apparatus, or device.
[0128] In the embodiments of the present disclosure, computer-executable instruction program codes for performing the operations of the present disclosure may be written in one or more programming languages or combinations thereof, and the programming languages include Java, C++, PHP, Python, etc. The program codes may be executed entirely on the user's computer, partially on the user's computer, executed as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or terminal. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (for example, by using an Internet service provider to connect through the Internet).
[0129] The above embodiments are only exemplary embodiments of the present disclosure and are not used to limit the present disclosure. The protection scope of the present disclosure is defined by the claims. Those skilled in the art can make various modifications or equivalent replacements within the essence and protection scope of the present disclosure, and such modifications or equivalent replacements should also be regarded as falling within the protection scope of the present disclosure.< / packagename> < / packagename>
Claims
1. A data storage method, comprising: Obtaining data to be stored during the operation of an application; Determining whether the data volume of the data to be stored is greater than a preset data volume threshold; If so, storing the data to be stored using a first storage unit in a first storage component; if not, storing the data to be stored using a second storage unit in the first storage component, wherein the first storage component is obtained by replacing a second storage component when the application starts, and the storage method of the second storage component is the same as that of the second storage unit; the second storage component is a native storage component, the first storage component is a storage component including a native storage method and a big data storage method, the first storage unit has a big data storage method, and the second storage unit retains the native storage method.
2. The method according to claim 1, wherein, When the application starts, replacing the second storage component with the first storage component, comprising: When the application starts, obtaining an instance of the application's application object; Based on the instance of the application object, obtaining the second storage component of the application through a reflection mechanism; Replacing the second storage component with the first storage component.
3. The method according to claim 2, wherein Based on the instance of the application object, obtaining the second storage component of the application through a reflection mechanism, comprising: Obtaining the package name of the application based on the instance of the application object; Obtaining the second storage component corresponding to the application from a preset storage variable based on the package name of the application, wherein the preset storage variable stores the package names and corresponding second storage components of multiple applications in a map structure.
4. The method according to claim 2, wherein, Replacing the second storage component with the first storage component, comprising: Determining the second storage instance of the second storage component as the second storage unit; Adding the first storage unit including the first storage instance to the second storage component to form the first storage component.
5. The method according to claim 2, wherein, The method further comprises: Creating a custom first storage instance based on the application; During the compilation stage of the application, encapsulating the first storage instance to obtain the first storage unit.
6. The method according to claim 5, wherein, The method further comprises: During the compilation stage of the application, encapsulating the first storage instance and the second storage instance corresponding to the second storage component to obtain the first storage component; When the application starts, replacing the second storage component with the first storage component.
7. The method according to any one of claims 1 to 6, wherein The first storage component is a SharedPreferences component, the first storage unit stores the data to be stored in the form of a file and / or a database, and the second storage unit stores the data to be stored in the form of xml.
8. A data storage device, comprising: An obtaining module configured to obtain data to be stored during the operation of an application; A judging module configured to judge whether the data volume of the data to be stored is greater than a preset data volume threshold; A storage module, configured to use a first storage unit in a first storage component to store the data to be stored if the amount of the data to be stored is greater than a preset data volume threshold; if the amount of the data to be stored is not greater than the preset data volume threshold, use a second storage unit in the first storage component to store the data to be stored, wherein the first storage component replaces a second storage component when the application is started, and the storage mode of the second storage component is the same as that of the second storage unit; the second storage component is a native storage component, the first storage component is a storage component including a native storage mode and a big data storage mode, the first storage unit has a big data storage mode, and the second storage unit retains the native storage mode.
9. An electronic device, comprising: A processor and a memory; The memory stores computer-executable instructions; When the processor executes the computer-executable instructions stored in the memory, the method according to any one of claims 1-7 is implemented.
10. A computer-readable storage medium, on which computer-executable instructions are stored, and when the computer-executable instructions are executed by a processor, the method according to any one of claims 1-7 is implemented.
Citation Information
Patent Citations
Data storage method and device of mobile terminal
CN106484691A
Hierarchical storage method and device, electronic equipment and computer readable storage medium
CN110018788A