LastPass clone - part 11: CoreData configuration

lastpass redesign May 25, 2020

Hey guys! In the previous part (part 10), we finished the most important part of the home screen, the only thing left is implementing the menu, but that will be implemented after after integrating CoreData. So in this part we will configure CoreData and in the next one we will finish implementing everything CoreData related. Let’s jump right in.

Preparations

You should have the source code link in your email inbox if you are subscribed, otherwise click here to subscribe and get the source code.

In order to follow along from here, you must have been using the project that I link above or if you have created a brand new project, you have checked the radio button that said CoreData, otherwise you will have to setup CoreData manually, it’s definitely not hard, but tedious. Refer to this article for guidance.

Data Model File and Entity Creation.

Data Model File

Inside your project, look for a file named SinglePass.xcdatamodeld. This is the file that we will use to create schemas which are NSManagedObject subclasses. We will create 2 entities, passwords and notes. Let’s do just that

Entity creation

Like I’ve just said, we will have 2 entities. I will show you how to create one, and you can replicate the process for the second one. select the SinglePass.xcdatamodeld data model file , then click the add button located at the bottom-left corner and rename the Entity to PasswordItem.

Then, while that entity is selected, click the add button in the attributes section to add an attribute.

In the above screenshot, we create the PasswordItem entity. Add those 8 attributes. When you’re done, select the inspector on the right and set the module to the current project module and set the codegen to Manual/None , also make sure the module is Current Product Module because we want to control the managedObject ourselves like this:

Now, do the same for the NoteItem, here is a screenshot for it.

We only have 6 attributes for this one. Make sure to not forget to set the codegen to Manual/None for both the PasswordItem and the NoteItem entities.

NSManaged objects creation.

After, we’ve done everything aforementioned successfully, it’s now time to create the NSManaged objects for the entities we’ve created. What is a managed object you may ask. Apple defines it as follow:

A managed object is a model object (in the model-view-controller sense) that represents a record from a persistent store. A managed object is an instance of NSManagedObject or a subclass of NSManagedObject. A managed object is registered with a managed object context. In any given context, there is at most one instance of a managed object that corresponds to a given record in a persistent store.

In the above statement, you can understand that the managed objects we’ve just created will be the ones that we will hold the data that we want save in the database (CoreData), and that they are model objects like in MVC (model-view-controller), but we are not going to use MVC in this project, we will use use MVVM, so we will need to map those 2 models, to their respective ViewModel Objects.

Now, create the following files in the Models folder: PasswordItem.swift and NoteItem.swift, and then add the following code inside them respectively:

import SwiftUI
import CoreData

public class PasswordItem: NSManagedObject, Identifiable {
    @NSManaged public var id: UUID
    @NSManaged public var site: String
    @NSManaged public var username: String
    @NSManaged public var password: String
    @NSManaged public var createdAt: Date
    @NSManaged public var note: String
    @NSManaged public var lastUsed: Date
    @NSManaged public var isFavorite: Bool
}

And


import Foundation
import CoreData

 public class NoteItem: NSManagedObject, Identifiable {
    @NSManaged public var id: UUID
    @NSManaged public var name: String
    @NSManaged public var content: String
    @NSManaged public var isFavorite: Bool
    @NSManaged public var createdAt: Date
    @NSManaged public var lastUsed: Date    
}

Those are our two model objects. Next up, let’s create their respective ViewModel objects. Create a ViewModels folder in the root directory, then add 2 files named PasswordViewModel.swift and NoteViewModel.swift. Next, put the following code inside each of them respectively:


import Foundation


struct PasswordViewModel: Identifiable {
    var id: UUID
    var site: String
    var username: String
    var password: String
    var createdAt: Date
    var note: String
    var lastUsed: Date
    var isFavorite: Bool
    
    init(passwordItem: PasswordItem) {
        self.id = passwordItem.id
        self.site = passwordItem.site
        self.username = passwordItem.username
        self.password = passwordItem.password
        self.createdAt = passwordItem.createdAt
        self.isFavorite = passwordItem.isFavorite
        self.note = passwordItem.note
        self.lastUsed = passwordItem.lastUsed
    }
}

And


import Foundation

struct NoteViewModel: Identifiable {
    var id: UUID
    var name: String
    var content: String
    var isFavorite: Bool
    var createdAt: Date
    var lastUsed: Date
    
    init(noteItem: NoteItem) {
        self.id = noteItem.id
        self.name = noteItem.name
        self.content = noteItem.content
        self.isFavorite = noteItem.isFavorite
        self.createdAt = noteItem.createdAt
        self.lastUsed = noteItem.lastUsed
    }
}

As you can see above, we map the items to their respective view models which may leave you wondering why on earth haven’t we just used the items directly? Is there anything that prevent us from doing so? The answer is nothing, we could’ve used the NSManaged object subclasses directly, but using viewmodels gives us more ways to control and customise properties in a cleaner manner.

This is it for this part folks, In this part we’ve only prepared the playground for what we will be doing in the next one. Stay tuned for more. Please feel free to share this article, subscribe if you haven’t done so already. If you have any question, send me an email. Stay tuned and happy coding.

John K

I am a software developer and code enthusiast. Do you want to work with me, have a suggestion or a request? Feel free to contact me at [email protected] or https://twitter.com/liquidcoder