{"id":10263,"date":"2023-06-23T01:43:43","date_gmt":"2023-06-23T08:43:43","guid":{"rendered":"https:\/\/www.couchbase.com\/developers\/tutorials\/quickstart-ios-uikit-query\/"},"modified":"2023-06-23T01:43:43","modified_gmt":"2023-06-23T08:43:43","slug":"quickstart-ios-uikit-query","status":"publish","type":"tutorials","link":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/","title":{"rendered":"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit"},"content":{"rendered":"\n<h3 id=\"h-introduction\" class=\"wp-block-heading\">Introduction<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Couchbase Lite brings powerful querying and Full-Text-Search(FTS) capabilties to the edge. The new query interface is based on <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.couchbase.com\/products\/n1ql\">N1QL<\/a>, Couchbase\u2019s declarative query language that extends <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/www.sqlite.org\/index.html\">SQL<\/a> for JSON. If you are familiar with SQL, you will feel right at home with the semantics of the new API.  The query API is designed using the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/en.wikipedia.org\/wiki\/Fluent_interface\">Fluent API Design Pattern<\/a>, and it uses method cascading to read to like a Domain Specific Language (DSL). This makes the interface very intuitive and easy to understand.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Couchbase Lite can be used as a standalone embedded database within your mobile app.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial will walk through a simple swift app that will<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Demonstrate how you can bundle, load and use a  <strong><em>prebuilt<\/em><\/strong> instance of Couchbase Lite<\/li>\n\n\n<li>Introduce you to the basics of the <code>QueryBuilder<\/code> interface<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can learn more about Couchbase Mobile <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/developer.couchbase.com\/mobile\">here<\/a>.<\/p>\n\n\n\n<h3 id=\"h-prerequisites\" class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial assumes familiarity with building Swift apps with Xcode and with the basics of Couchbase Lite.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p>If you are unfamiliar with the basics of Couchbase Lite, it is recommended that you walk through the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/developer.couchbase.com\/tutorial-quickstart-ios-uikit-basic\">Quickstart in Couchbase Lite with iOS, Swift, and UIKit<\/a> on using Couchbase Lite as a standalone database<\/p>\n<\/li>\n\n\n<li>\n<p>iOS (Xcode 12\/13) &#8211; Download latest version from the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/itunes.apple.com\/us\/app\/xcode\/id497799835?mt=12\">Mac App Store<\/a> or via <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/github.com\/RobotsAndPencils\/XcodesApp\">Xcodes<\/a><\/p>\n<\/li>\n\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Note<\/strong>: If you are using an older version of Xcode, which you need to retain for other development needs, make a copy of your existing version of Xcode and install the latest Xcode version.  That way you can have multiple versions of Xcode on your Mac.  More information can be found in <a href=\"https:\/\/developer.apple.com\/library\/archive\/technotes\/tn2339\/_index.html#\/\/apple_ref\/doc\/uid\/DTS40014588-CH1-I_HAVE_MULTIPLE_VERSIONS_OF_XCODE_INSTALLED_ON_MY_MACHINE__WHAT_VERSION_OF_XCODE_DO_THE_COMMAND_LINE_TOOLS_CURRENTLY_USE_\">Apple&#8217;s Developer Documentation<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<h3 id=\"h-app-overview\" class=\"wp-block-heading\">App Overview<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We will be working with a very simple &#8220;User Profile&#8221; app. If you had walked through the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/developer.couchbase.com\/tutorial-quickstart-ios-uikit-query\">Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit tutorial<\/a>, you will recognize that this version extends the functionality introduced in the app introduced in that tutorial.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This app does the following<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Allows users to log in and create or update his\/her user profile information. You could do that in the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/developer.couchbase.com\/tutorial-quickstart-ios-uikit-query\">Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit tutorial<\/a><\/li>\n\n\n<li>Includes a second record type. As part of profile information, users can now select a &#8220;university&#8221; from a list of possible options.<\/li>\n\n\n<li>The list of matching universities is found by quering (using the new Query API) a local prebuilt &#8220;University&#8221; Database, which is bundled in the app.<\/li>\n\n\n<li>The user profile information is persisted as a Document in the local Couchbase Lite Database. So subsquently, when the user logs out and logs back in again, the profile information is loaded from the Database.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/university_app_overview.gif\" alt=\"App Overview\"><\/p>\n\n\n\n<h3 id=\"h-installation\" class=\"wp-block-heading\">Installation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To clone the project from GitHub, type the following command in your terminal<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>git clone https:\/\/github.com\/couchbase-examples\/ios-swift-cblite-userprofile-query\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-installing-couchbase-lite-xcframework\" class=\"wp-block-heading\">Installing Couchbase Lite XCFramework<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we will download the Couchbase Lite 3.0 XCFramework.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Couchbase Lite iOS XCFramework is distributed via SPM, CocoaPods, Carthage, or you can download the pre-built framework.  See the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/docs.couchbase.com\/couchbase-lite\/3.0\/swift\/gs-install.html\"> Getting Started &#8211; Install<\/a> documentation for more information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our example, we will be downloading the pre-built version of the XCFramework, using a script. To do this, type the following in a command terminal:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>  cd \/path\/to\/UserProfileQueryDemo\/src\n\n  sh install_tutorial.sh\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s verify the installation.<\/p>\n\n\n\n<h4 id=\"h-try-it-out\" class=\"wp-block-heading\">Try it Out<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <code>UserProfileQueryDemo.xcodeproj<\/code>. The project would be located at <code> \/path\/to\/UserProfileQueryDemo\/src<\/code><\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>open UserProfileQueryDemo.xcodeproj\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build and run the project using the <em>simulator<\/em> in Xcode. While you can run the app on a real device, we recommend the Simulator so you can see the debug logs in the output console.<\/li>\n\n\n<li>Verify that you see the login screen<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/user_profile_login.png\" alt=\"User Profile Login Screen Image\" title=\"#width=300px\"><\/p>\n\n\n\n<h3 id=\"h-data-model\" class=\"wp-block-heading\">Data Model<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Couchbase Lite is a JSON Document Store. A Document is a logical collection of named fields and values. The values are any valid JSON types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to the standard JSON types, Couchbase Lite supports some special types like Date and Blob. While it is not required or enforced, it is a recommended practice to include a &#8220;type&#8221; property that can serve as a namespace for related documents.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The app deals with two Document types: User Profile and University.<\/p>\n\n\n\n<h4 id=\"h-the-user-profile-document\" class=\"wp-block-heading\">The &#8220;User Profile&#8221; Document<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The User Profile Document has a type property of user.Its document ID is of the form &#8220;user::<a href=\"mailto:demo@example.com\">demo@example.com<\/a>&#8220;.  An example of a document would be<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n    &quot;type&quot;:&quot;user&quot;,\n    &quot;name&quot;:&quot;Jane Doe&quot;,\n    &quot;email&quot;:&quot;jame.doe@earth.org&quot;,\n    &quot;address&quot;:&quot;101 Main Street&quot;,\n    &quot;image&quot;:CBLBlob (image\/jpg),\n    &quot;university&quot;:&quot;Rensselaer Polytechnic&quot;\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-userrecord\" class=\"wp-block-heading\">UserRecord<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><em>&#8220;user&#8221;<\/em><\/strong> Document is encoded to a native struct named <strong><em>UserRecord<\/em><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>let kUserRecordDocumentType = &quot;user&quot;\ntypealias ExtendedData = [[String:Any]]\nstruct UserRecord : CustomStringConvertible{\n  let type = kUserRecordDocumentType\n  var name:String?\n  var email:String?\n  var address:String?\n  var imageData:Data?\n  var extended:ExtendedData? \n  \n    var description: String {\n      return &quot;name = (String(describing: name)), \n      email = (String(describing: email)), \n      address = (String(describing: address)), \n      imageData = (imageData?.debugDescription \n      ?? &quot; &quot;)&quot;\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-the-university-document\" class=\"wp-block-heading\">The &#8220;University&#8221; Document<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The app comes bundled with a collection of Documents of type <strong><em>&#8220;university&#8221;<\/em><\/strong>. Each Document represents a university.<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n    &quot;type&quot;:&quot;university&quot;,&quot;web_pages&quot;: [\n      &quot;http:\/\/www.rpi.edu\/&quot;\n    ],\n    &quot;name&quot;: &quot;Rensselaer Polytechnic Institute&quot;,\n    &quot;alpha_two_code&quot;: &quot;US&quot;,\n    &quot;state-province&quot;: null,\n    &quot;domains&quot;: [\n      &quot;rpi.edu&quot;\n    ],\n    &quot;country&quot;: &quot;United States&quot;\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-universityrecord\" class=\"wp-block-heading\">UniversityRecord<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong><em>&#8220;university&#8221;<\/em><\/strong> Document is encoded to a native struct named <strong><em>UniversityRecord<\/em><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>typealias Universities = [UniversityRecord]\n\/\/ Native object\nstruct UniversityRecord : CustomStringConvertible{\n  var alphaTwoCode:String?\n  var country:String?\n  var domains:[String]?\n  var name:String?\n  var webPages:[String]?\n  \nvar description: String {\n      return &quot;name = (String(describing: name)), country = (String(describing: country)), domains = (String(describing: domains)), webPages = (webPages), alphaTwoCode = (String(describing: alphaTwoCode)) &quot;\n  }\n}\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-using-a-prebuilt-database\" class=\"wp-block-heading\">Using a Prebuilt Database<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There are several reasons why you may want to bundle your app with a prebuilt database. This would be suited for data that does not change or change that often, so you can avoid the bandwidth and latency involved in fetching\/syncing this data from a remote server. This also improves the overall user experience by reducing the start-up time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our app, the instance of Couchbase Lite that holds the pre-loaded &#8220;university&#8221; data is separate from the Couchbase Lite instance that holds &#8220;user&#8221; data hold the pre-loaded data. A separate Couchbase Lite instance is not required. However, in our case, since there can be many users potentially using the app on a given device, it makes more sense to keep it separate. This is to avoid duplication of pre-loaded data for every user.<\/p>\n\n\n\n<h4 id=\"h-location-of-the-cblite-file\" class=\"wp-block-heading\">Location of the cblite file<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The pre-built database will be in the form of a <code>cblite<\/code> file. It should be be in your app project bundle<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the <code>UserProfileQueryDemo.xcodeproj<\/code> project explorer, locate the <code>universities.cblite2<\/code> file<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/cblite_location.png\" alt=\"Prebuilt Database Location\"><\/p>\n\n\n\n<h4 id=\"h-loading-the-prebuilt-database\" class=\"wp-block-heading\">Loading the Prebuilt Database<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <strong>DatabaseManager.swift<\/strong> file and locate the <code>openPrebuiltDatabase()<\/code> function. The prebuilt database is common to all users of the app (on the device). So it will be loaded once and shared by all users on the device.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>func openPrebuiltDatabase(handler:(_ error:Error?)-&gt;Void) {\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, we create an instance of <code>DatabaseConfiguration<\/code> object and specify the path where the database would be located<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>var options = DatabaseConfiguration()\nguard let universityFolderUrl = _applicationSupportDirectory else {\n  fatalError(&quot;Could not open Application Support Directory for app!&quot;)\n  return\n}\nlet universityFolderPath = universityFolderUrl.path\nlet fileManager = FileManager.default\nif !fileManager.fileExists(atPath: universityFolderPath) {\n  try fileManager.createDirectory(atPath: universityFolderPath,\n                                  withIntermediateDirectories: true,\n                                  attributes: nil)\n\n}\n\/\/ Set the folder path for the CBLite DB\noptions.directory = universityFolderPath\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Then we determine if the &#8220;universities&#8221; database already exists at the specified location. It would not be present if this is the first time we are using the app, in which case, we locate the <strong><em>&#8220;universities.cblite&#8221;<\/em><\/strong> resource in the App&#8217;s main bundle and we copy it over to the Database folder.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If the database is already present at the specified Database location, we simply open the database<\/p>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>\/\/ Load the prebuilt &quot;universities&quot; database if it does not exist as the specified folder\nif Database.exists(withName: kUniversityDBName, inDirectory: universityFolderPath) == false\n{\n  \/\/ Load prebuilt database from App Bundle and copy over to Applications support path\n  if let prebuiltPath = Bundle.main.path(forResource: kUniversityDBName, ofType: &quot;cblite2&quot;) {\n      try Database.copy(fromPath: prebuiltPath, toDatabase: &quot;(kUniversityDBName)&quot;, withConfig: options)\n  }\n  \/\/ Get handle to DB  specified path\n  _universitydb = try Database(name: kUniversityDBName, config: options)\n\n\/\/ Create indexes to facilitate queries\n  try createUniversityDatabaseIndexes()\n}\nelse\n{\n  \/\/ Gets handle to existing DB at specified path\n  _universitydb = try Database(name: kUniversityDBName, config: options)\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-indexing-the-prebuilt-database\" class=\"wp-block-heading\">Indexing the Prebuilt Database<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p>Creating indexes for non-FTS based queries is optional. However, in order to speed up queries, you can create indexes on the properties that you would query against. Indexing is handled eagerly.<\/p>\n<\/li>\n\n\n<li>\n<p>In the <strong>DatabaseManager.swift<\/strong> file, locate the <code>createUniversityDatabaseIndexes()<\/code> function. We create an index on the <code>name<\/code> and <code>location<\/code> properties of the documents in the <strong><em>university<\/em><\/strong> database.<\/p>\n<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>fileprivate func createUniversityDatabaseIndexes()throws {\n  \/\/ For searches on type property\n  try _universitydb?.createIndex(IndexBuilder.valueIndex(items:  ValueIndexItem.expression(Expression.property(&quot;name&quot;)),ValueIndexItem.expression(Expression.property(&quot;location&quot;))), withName: &quot;NameLocationIndex&quot;)\n}\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-closing-the-database\" class=\"wp-block-heading\">Closing the Database<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When a user logs out, we close the Prebuilt Database along with other user-specific databases<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the <strong>DatabaseManager.swift<\/strong> file, locate the <code>closePrebuiltDatabase()<\/code> function.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>func closePrebuiltDatabase() -&gt; Bool {\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Closing the database is pretty straightforward<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>try db.close()\ntry universitydb.close()\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-try-it-out\" class=\"wp-block-heading\">Try It Out<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The app should be running in the simulator<\/li>\n\n\n<li>Log into the app with any email Id and password. Let&#8217;s use the values <strong><em>&#8220;<a href=\"mailto:demo@example.com\">demo@example.com<\/a>&#8220;<\/em><\/strong> and <strong><em>&#8220;password&#8221;<\/em><\/strong> for user Id and password fields respectively. If this is the first time that <strong><em>any<\/em><\/strong> user is signing in to the app, the pre-built database will be loaded from the App Bundle. In addition, new user-specific Database will be created \/ opened.<\/li>\n\n\n<li>Confirm that the console log output has a message similar to the one below. This output also indicates the location of the Prebuilt database as well as the Database for the user. This would be within the Application Support folder.  In this example, we are logging in with a user email Id of <strong><em>&#8220;<a href=\"mailto:demo@example.com\">demo@example.com<\/a>&#8220;<\/em><\/strong>.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>Will open Prebuilt DB  at path \/Users\/priya.rajagopal\/Library\/Developer\/CoreSimulator\/Devices\/E4E62394-9940-4AF8-92FC-41E3C794B216\/data\/Containers\/Data\/Application\/A9425551-7F52-461D-B4F5-CC04315154D6\/Library\/Application Support\n\n2018-05-04 17:04:16.319360-0400 UserProfileDemo[54115:13479070] CouchbaseLite\/2.0.0 (Swift; iOS 11.3; iPhone) Build\/806 Commit\/2f2a2097+CHANGES LiteCore\/2.0.0 (806)\n\n2018-05-04 17:04:16.319721-0400 UserProfileDemo[54115:13479070] CouchbaseLite minimum log level is Verbose\nWill open\/create DB  at path \/Users\/priya.rajagopal\/Library\/Developer\/CoreSimulator\/Devices\/E4E62394-9940-4AF8-92FC-41E3C794B216\/data\/Containers\/Data\/Application\/A9425551-7F52-461D-B4F5-CC04315154D6\/Library\/Application Support\/demo@example.com\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The above log message indicate the location of the Prebuilt database as well as the Database for the user. This would be within the <em>Application Support<\/em> folder.<\/li>\n\n\n<li>Open the folder in your Finder app and verify that a Database with name <em>&#8220;univerities&#8221;<\/em> exists along with a user specific Database with name <em>&#8220;userprofile&#8221;<\/em><\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/db_locations.png\" alt=\"Database Locations\"><\/p>\n\n\n\n<h3 id=\"h-exploring-the-query-api\" class=\"wp-block-heading\">Exploring the Query API<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Query API in Couchbase Lite is extensive. In our app, we will be using the <code>QueryBuilder<\/code> API to make a simple <em>pattern matching<\/em> query using the <code>like<\/code> operator.<\/p>\n\n\n\n<h4 id=\"h-fetching-university-document\" class=\"wp-block-heading\">Fetching University Document<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">From the &#8220;Your Profile&#8221; screen, when the user taps on the &#8220;Select University&#8221; button, a search screen is displayed where the user can enter the search criteria (name and optionally, the location) for the university.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the search criteria is entered, the local <em>&#8220;universities&#8221;<\/em> Database is queried for <a href=\"#the-university-document\">The &#8220;University&#8221; Document<\/a> documents that match the specified search criteria.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Open the <strong>UniversityPresenter.swift<\/strong> file and locate the <code>fetchUniversityRecords()<\/code> function.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>func fetchUniversitiesMatchingName( _name:String,country countryStr:String?, handler:@escaping(_ universities:Universities?, _ error:Error?)-&gt;Void)\n{\n  do {\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We build the Query using the <code>QueryBuilder<\/code> API that will look for Documents that match the specified criteria.<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code> var whereQueryExpr = Function.lower(Expression.property(UniversityDocumentKeys.name.rawValue))\n  .like(Expression.string(&quot;%(name.lowercased())%&quot;))\n\nif let countryExpr = countryStr {\n  let countryQueryExpr = Function.lower(Expression.property(UniversityDocumentKeys.country.rawValue))\n      .like(Expression.string(&quot;%(countryExpr.lowercased())%&quot;))\n  whereQueryExpr = whereQueryExpr.and(countryQueryExpr))\n}\n\nlet universityQuery = QueryBuilder.select(SelectResult.all()) \n  .from(DataSource.database(db)) \n  .where(whereQueryExpr)\n\nprint(try? universityQuery.explain())\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p>Build a <code>QueryExpression<\/code> that uses the <code>like<\/code> operator to look for the specified <strong><em>&#8220;name&#8221;<\/em><\/strong> string in the <strong><em>&#8220;name&#8221;<\/em><\/strong> property. Notice couple of things here: (a) The use of <strong>wildcard &#8220;%&#8221; operator<\/strong> to denote that we are looking for the presence of the string anywhere in the <strong><em>&#8220;name&#8221;<\/em><\/strong> property and (b) The use of <code>Function.lowercase()<\/code> to convert the search string into lowercase equivalent. Since <code>like<\/code> operator does <strong><em>case-senstive matching<\/em><\/strong>, we convert the search string and the property value to lowercase equivalents and compare the two.<\/p>\n<\/li>\n\n\n<li>\n<p>If the location criteria was specified in the search, then Build a <code>QueryExpression<\/code> that uses the <code>like<\/code> operator to look for the specified <strong><em>&#8220;location&#8221;<\/em><\/strong> string in the <strong><em>&#8220;location&#8221;<\/em><\/strong> property.<\/p>\n<\/li>\n\n\n<li>\n<p>The <code>SelectResult.all()<\/code> specifiees that we are interested in all properties in Documents that match the specified criteria<\/p>\n<\/li>\n\n\n<li>\n<p>The <code>DataSource.database(db)<\/code> specified the Data Source<\/p>\n<\/li>\n\n\n<li>\n<p>We include the <code>where<\/code> clause that is the logical ANDing of the QueryExpression.<\/p>\n<\/li>\n\n\n<li>\n<p>We run the Query by calling the <code>execute()<\/code> method on the Query that was constructed in the previous step<\/p>\n<\/li>\n\n<\/ul>\n\n\n\n<pre class=\"wp-block-code language-swift\"><code>var universities = Universities()\n\nfor result in try universityQuery.execute() {\n  if let university = result.dictionary(forKey: &quot;universities&quot;){\n\n    var universityRecord = UniversityRecord()\n    universityRecord.name =  university.string(forKey: UniversityDocumentKeys.name.rawValue) \n    universityRecord.country  =  university.string(forKey: UniversityDocumentKeys.country.rawValue)\n    universityRecord.webPages  =  university.array(forKey: UniversityDocumentKeys.webPages.rawValue)?.toArray() as? [String] \n\n    universities.append(universityRecord)\n  }\n}\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create an instance of <a href=\"#university-record\">UniversityRecord<\/a> type<\/li>\n\n\n<li>Use specific type getters to fetch property values. These<a href=\"#university-record\">UniversityRecord<\/a> instance is populated with these property values.<\/li>\n\n\n<li>Getters also available for <code>array<\/code> types. This returns a Couchbase Lite <code>ArrayObject<\/code> type. So you would have to use <code>toArray()<\/code> to convert the Couchbase Lite native array equivalent.<\/li>\n\n<\/ul>\n\n\n\n<h4 id=\"h-try-it-out\" class=\"wp-block-heading\">Try It Out<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You should have followed the steps discussed in the &#8220;Try It Out&#8221; section under <a href=\"#loading-the-prebuilt-database\">Loading the Prebuilt Database<\/a><\/li>\n\n\n<li>Tap on &#8220;Select University&#8221; button<\/li>\n\n\n<li>You should see a screen show that allows you enter the search criteria for the university<\/li>\n\n\n<li>Enter &#8220;Harv&#8221; for name . You can optionally enter &#8220;united states&#8221; for location<\/li>\n\n\n<li>Confirm that you see a list of universities that match the criteria<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/university_list.gif\" alt=\"University List\"><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Select a university<\/li>\n\n\n<li>Press &#8220;Done&#8221; button<\/li>\n\n\n<li>Confirm that the university you selected shows up in the University label<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/university_selection.gif\" alt=\"University Selection\"><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can optionally fill in other entries in the User Profile screen<\/li>\n\n\n<li>Tap &#8220;Done&#8221; button<\/li>\n\n\n<li>Confirm that you see an alert message &#8220;Succesfully Updated Profile&#8221;. The Document will be updated this time.<\/li>\n\n\n<li>Tap &#8220;Log Off&#8221; and log out of the app<\/li>\n\n\n<li>Log back into the app with the same user email Id and password that you used earlier. In my example, I used <strong><em>&#8220;<a href=\"mailto:demo@example.com\">demo@example.com<\/a>&#8220;<\/em><\/strong> and <strong><em>&#8220;password&#8221;<\/em><\/strong>. So I will log in with those credentials again.<\/li>\n\n\n<li>Confirm that you see the profile screen  with the <strong><em>university<\/em><\/strong> value that you set earlier.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><img decoding=\"async\" src=\".\/profile_update.gif\" alt=\"Log Off and Log Back On\"><\/p>\n\n\n\n<h3 id=\"h-learn-more\" class=\"wp-block-heading\">Learn More<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Congratulations on completing this tutorial!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial walked you through an example of how to use a pre-built Couchbase Lite database. We looked at a simple Query example. Check out the following links for further details on the Query API including a Xcode playground for testing the APIs.<\/p>\n\n\n\n<h4 id=\"h-further-reading\" class=\"wp-block-heading\">Further Reading<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/blog.couchbase.com\/sql-for-json-query-interface-couchbase-mobile\/\">Fundamentals of the Couchbase Lite Query API<\/a><\/li>\n\n\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/blog.couchbase.com\/querying-array-collections-couchbase-mobile\/\">Handling Arrays in Queries<\/a><\/li>\n\n\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/blog.couchbase.com\/full-text-search-couchbase-mobile-2-0\/\">Couchbase Lite Full Text Search API<\/a><\/li>\n\n\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https:\/\/blog.couchbase.com\/join-queries-couchbase-mobile\/\">Couchbase Lite JOIN Query<\/a><\/li>\n\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Couchbase Lite brings powerful querying and Full-Text-Search(FTS) capabilties to the edge. The new query interface is based on N1QL, Couchbase\u2019s declarative query language that extends SQL for JSON. If you are familiar with SQL, you will feel right at home with the semantics of the new API. The query API is designed using the Fluent API Design Pattern, and it uses method cascading to read to like a Domain Specific Language (DSL). This makes the interface very intuitive and easy to understand. Couchbase Lite can be used as a standalone embedded database within your mobile app. This tutorial will walk through a simple swift app that will You can [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_acf":"","footnotes":""},"tutorial-languages":[594],"tutorial-tags":[590],"tutorial-technologies":[573,585,549],"tutorial-types":[550],"class_list":["post-10263","tutorials","type-tutorials","status-publish","hentry","tutorial-languages-swift","tutorial-tags-ios","tutorial-technologies-fts","tutorial-technologies-mobile","tutorial-technologies-query","tutorial-types-quickstart"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit - Couchbase<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/\" \/>\n<meta property=\"og:locale\" content=\"ja_JP\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit\" \/>\n<meta property=\"og:description\" content=\"Introduction Couchbase Lite brings powerful querying and Full-Text-Search(FTS) capabilties to the edge. The new query interface is based on N1QL, Couchbase\u2019s declarative query language that extends SQL for JSON. If you are familiar with SQL, you will feel right at home with the semantics of the new API. The query API is designed using the Fluent API Design Pattern, and it uses method cascading to read to like a Domain Specific Language (DSL). This makes the interface very intuitive and easy to understand. Couchbase Lite can be used as a standalone embedded database within your mobile app. This tutorial will walk through a simple swift app that will You can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/\" \/>\n<meta property=\"og:site_name\" content=\"Couchbase\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Couchbase\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2548\" \/>\n\t<meta property=\"og:image:height\" content=\"1171\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@couchbase\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"13\u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/developers\\\/tutorials\\\/quickstart-ios-uikit-query\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/developers\\\/tutorials\\\/quickstart-ios-uikit-query\\\/\",\"name\":\"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit - Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/#website\"},\"datePublished\":\"2023-06-23T08:43:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/developers\\\/tutorials\\\/quickstart-ios-uikit-query\\\/#breadcrumb\"},\"inLanguage\":\"ja\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/developers\\\/tutorials\\\/quickstart-ios-uikit-query\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/developers\\\/tutorials\\\/quickstart-ios-uikit-query\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/\",\"name\":\"Couchbase\",\"description\":\"The Operational Data Platform for AI\u2122\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/#organization\"},\"alternateName\":\"Couchbase, Inc.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ja\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/#organization\",\"name\":\"Couchbase\",\"alternateName\":\"Couchbase, Inc.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ja\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"width\":1129,\"height\":1129,\"caption\":\"Couchbase\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/ja\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Couchbase\\\/\",\"https:\\\/\\\/x.com\\\/couchbase\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/CouchbaseServer\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/couchbase\\\/\",\"https:\\\/\\\/github.com\\\/couchbase\\\/\",\"https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Couchbase,_Inc.\"],\"description\":\"Couchbase is a database software company headquartered in San Jose, California, providing an operational data platform for AI used by hundreds of global enterprises.\",\"legalName\":\"Couchbase, Inc.\",\"foundingDate\":\"2011-01-01\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit - Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/","og_locale":"ja_JP","og_type":"article","og_title":"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit","og_description":"Introduction Couchbase Lite brings powerful querying and Full-Text-Search(FTS) capabilties to the edge. The new query interface is based on N1QL, Couchbase\u2019s declarative query language that extends SQL for JSON. If you are familiar with SQL, you will feel right at home with the semantics of the new API. The query API is designed using the Fluent API Design Pattern, and it uses method cascading to read to like a Domain Specific Language (DSL). This makes the interface very intuitive and easy to understand. Couchbase Lite can be used as a standalone embedded database within your mobile app. This tutorial will walk through a simple swift app that will You can [&hellip;]","og_url":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/","og_site_name":"Couchbase","article_publisher":"https:\/\/www.facebook.com\/Couchbase\/","og_image":[{"width":2548,"height":1171,"url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/10\/Cloud-2.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@couchbase","twitter_misc":{"Est. reading time":"13\u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/","url":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/","name":"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit - Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/ja\/#website"},"datePublished":"2023-06-23T08:43:43+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/#breadcrumb"},"inLanguage":"ja","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/ja\/developers\/tutorials\/quickstart-ios-uikit-query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/"},{"@type":"ListItem","position":2,"name":"Quickstart in Couchbase Lite Query with iOS, Swift, and UIKit"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/ja\/#website","url":"https:\/\/www.couchbase.com\/ja\/","name":"Couchbase","description":"The Operational Data Platform for AI\u2122","publisher":{"@id":"https:\/\/www.couchbase.com\/ja\/#organization"},"alternateName":"Couchbase, Inc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/ja\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ja"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/ja\/#organization","name":"Couchbase","alternateName":"Couchbase, Inc.","url":"https:\/\/www.couchbase.com\/ja\/","logo":{"@type":"ImageObject","inLanguage":"ja","@id":"https:\/\/www.couchbase.com\/ja\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","contentUrl":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","width":1129,"height":1129,"caption":"Couchbase"},"image":{"@id":"https:\/\/www.couchbase.com\/ja\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Couchbase\/","https:\/\/x.com\/couchbase","https:\/\/www.youtube.com\/c\/CouchbaseServer","https:\/\/www.linkedin.com\/company\/couchbase\/","https:\/\/github.com\/couchbase\/","https:\/\/en.wikipedia.org\/wiki\/Couchbase,_Inc."],"description":"Couchbase\u306f\u30ab\u30ea\u30d5\u30a9\u30eb\u30cb\u30a2\u5dde\u30b5\u30f3\u30ce\u30bc\u306b\u672c\u793e\u3092\u7f6e\u304f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30bd\u30d5\u30c8\u30a6\u30a7\u30a2\u4f01\u696d\u3067\u3001\u6570\u767e\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u4f01\u696d\u304c\u4f7f\u7528\u3059\u308bAI\u7528\u306e\u904b\u7528\u30c7\u30fc\u30bf\u30d7\u30e9\u30c3\u30c8\u30d5\u30a9\u30fc\u30e0\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002.","legalName":"Couchbase, Inc.","foundingDate":"2011-01-01"}]}},"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorials\/10263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/types\/tutorials"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorials\/10263\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/media?parent=10263"}],"wp:term":[{"taxonomy":"tutorial-languages","embeddable":true,"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorial-languages?post=10263"},{"taxonomy":"tutorial-tags","embeddable":true,"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorial-tags?post=10263"},{"taxonomy":"tutorial-technologies","embeddable":true,"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorial-technologies?post=10263"},{"taxonomy":"tutorial-types","embeddable":true,"href":"https:\/\/www.couchbase.com\/ja\/wp-json\/wp\/v2\/tutorial-types?post=10263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}