JSONStore - Objective-C API
Follow along with the code sample:
- Download the compressed file with the code sample that is associated with this tutorial.
- Open the Basic.m file at
JSONStoreAPINativeiOS/Examples/Basic.m. The sample image provides context.
- Run the application by using Xcode. On the top bar, look for Product and then select Test. Alternatively, you can use the keyboard shortcut (⌘U).
Note: The code sample uses the built-in Unit Test framework of the Xcode IDE. Explaining how this framework works is beyond the scope of this tutorial.
Code Sample Walkthough
If an error occurs, the error variable contains information about the error, such as the error code. Otherwise, it remains
destroy API removes all JSONStore content from the application. It is used here to start with no data. Doing it this way ensures that the output is predictable in the code sample.
To persist data, you must first define at least one collection. These collections are entities that hold data. You can see here the definition of a collection that is called
Search fields are fields that are indexed inside a collection. You can use those fields when you search for data that is inside a collection.
You can see here the definition of two search fields:
The data types, such as
boolean, are used to better store input data.
open API is used to open one or more collections. If the collection was never opened before, a file is created on the file system to persist data that is inside the collection. Before the operation finishes, an accessor to that file is created.
The accessor allows the caller to call collection-level APIs such as
findAll, which are shown later in this code sample walkthrough.
The data that is stored inside the
people collection is defined here. Notice that the data is a hardcoded array of two JSON objects with key value pairs for
age. This data can be acquired from multiple sources (for example: Network Request, File I/O, User Input).
The collection accessor provides access to store data inside the
people collection. The input data must be in JSON format.
There are a couple of different ways to find documents inside a JSONStore collection (for example:
The easiest way, and the way that is shown here, is by using the
findAll API. This method returns all the data that is stored inside a collection.
Data that is stored inside a collection is called a document. Documents have
json key value pairs. The
_id pair is an internal identifier that is added automatically when data is added. The json pair contains all the data that was added.
To execute the tests, select Product > Test.
When the tests are executed, the output looks similar to the sample image.
The green check mark next to the test indicates that everything is working as expected.
For more information
For more information about JSONStore, see the product user documentation.▲