如何为我的应用程序制作离线数据库?

How to make offline database for my app?

我在使用 RestAPI 和保存离线数据时遇到问题。有没有人对此有最好的想法。

我想将来自服务器的 JSON 响应保存在本地数据库中,并在没有互联网可用时使用该数据。像 Firebase 实时数据库。

我认为您要求的是一种将数据持久保存到您的应用程序中的机制。有几种方法可以实现这一点。虽然它可能过于宽泛而无法提供详细的答案,但您可能需要检查选项:

数据库:

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores. The data can be manipulated using higher level objects representing entities and their relationships. Core Data manages the serialized version, providing object lifecycle and object graph management, including persistence. Core Data interfaces directly with SQLite, insulating the developer from the underlying SQL.

Wikipedia Resource.

Programming Guide.

SQLite is a relational database management system contained in a C programming library. In contrast to many other database management systems, SQLite is not a client–server database engine. Rather, it is embedded into the end program.

Wikipedia resource.

Realm is an open-source object database management system, initially for mobile (Android/iOS), also available for platforms such as Xamarin or React Native, and others, including desktop applications (Windows), and is licensed under the Apache License.

Wikipedia resource.


其他选择:

The UserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed. Apps store these preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults because they’re commonly used to determine an app’s default state at startup or the way it acts by default.

  • 将数据保存到 plist 个文件:

In the macOS, iOS, NeXTSTEP, and GNUstep programming frameworks, property list files are files that store serialized objects. Property list files use the filename extension .plist, and thus are often referred to as p-list files.

Property list files are often used to store a user's settings. They are also used to store information about bundles and applications, a task served by the resource fork in the old Mac OS.

  • 将数据保存到 json 个文件:

In computing, JavaScript Object Notation or JSON is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format used for asynchronous browser–server communication, including as a replacement for XML in some AJAX-style systems.

Wikipedia resource.