Nows-a days offline mode is a very common feature in app development. Every developer wants to run minimum app functionality without internet and device connect with internet all data automatically sync on remote DB. CoreData, SQLite, Firebase, etc. is handling offline functionality. Today we discuss about SQLite using swift.
What is SQLite?
SQLite is a C-language library that implements a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. It is built into all mobile phones and most computers and comes bundled inside countless other applications that people use every day. Source: https://www.sqlite.org/
Let’s implement SQLite in swift for offline applications.
In this example, we will use the FMDatabase library to access SQLite.
Swift version: 5.2 iOS version: 14 Xcode: 12.0
First Create a project from Xcode.
File ▸ New ▸ Project…. Choose the iOS ▸ Application ▸ Single View App template and create a new project.
Step 1: First copy .sqlite file from FileManager to application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
classUtil:NSObject{
func getPath(_fileName:String)->String{
let documentsURL=FileManager.default.urls(for:.documentDirectory,in:.userDomainMask)[0]
let fileURL=documentsURL.appendingPathComponent(fileName)
returnfileURL.path
}
func copyFile(_fileName:NSString){
let dbPath:String=getPath(fileName asString)
let fileManager=FileManager.default
if!fileManager.fileExists(atPath:dbPath){
let documentsURL=Bundle.main.resourceURL
let fromPath=documentsURL!.appendingPathComponent(fileName asString)
Specializes in Core Data, GCD, Swift and Storyboards. Expert in Firebase Push Notifications, threading and performance tuning. Proficient in managing Apple Developer Account and optimizing iOS app performance.
Be the first to comment.