Swift class inheritance initializer error - swift

What do I need to add to extend the UserModel with AuthenticatedUserModel without getting this error?
// 'required' initializer 'init(from:)' must be provided by subclass of 'UserModel'
I know I can also just add the accessToken to the UserModel as optional but I want to understand what's going on so I can understand swift a bit better?
class UserModel: Codable, Identifiable, ObservableObject {
let id: Int
let firstName: String?
let lastName: String?
let username: String?
let bio: String?
let theme: String?
let imageSrc: String?
let interests: [String]?
let followerCount: Int?
let following: Bool?
let followingCount: Int?
let hasCompletedRegistration: Bool?
let isPrivate: Bool?
let readerMode: Bool?
let isActive: Bool?
let isVerified: Bool?
let isSuspended: Bool?
let isAdmin: Bool?
let isFollowing: Bool?
let createdAt: String?
let updatedAt: String?
init(id: Int, firstName: String, lastName: String, bio: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
self.username = ""
self.bio = ""
self.theme = ""
self.imageSrc = ""
self.interests = [""]
self.followerCount = 0
self.following = false
self.followingCount = 0
self.hasCompletedRegistration = true
self.isPrivate = false
self.readerMode = true
self.isActive = true
self.isVerified = false
self.isSuspended = false
self.isAdmin = false
self.isFollowing = false
self.createdAt = ""
self.updatedAt = ""
}
var name: String {
return "\(firstName ?? "") \(lastName ?? "")"
}
}
class AuthenticatedUserModel: UserModel {
let accessToken: String?
override init(id: Int, firstName: String, lastName: String, bio: String) {
self.accessToken = nil
super.init(id: id, firstName: firstName, lastName: lastName, bio: bio)
}
}

The Decodable protocol requires the implementation of the method init(from:). Cause of adding a new property the automatically created init(from:) method of the parent class is not inherited. This happens cause the inherited method can not initialize the new property from the child class.
Therefore the solution is to add the required method. For example like this (untested code)
required init(from decoder: Decoder) throws {
accessToken = try decoder.singleValueContainer().decode(String.self)
try super.init(from: decoder)
}

Related

How can I fetch data from a Firestore reference?

I have the chat collection that has the following fields: hasUnreadMessage as a Bool, isActive as a Bool, person as a reference to
person collection, messages as an array of references to message collection.
Here are some screenshots
I want to create a function to fetch all the messages but for example in the person reference when I print directly the call of the imgString or name it's correct, but when I add them to the ChatModel they are missing.
Here is the function that I created.
func fetchMessages() {
db.collection("chat").getDocuments { snapshot, err in
if let error = err {
debugPrint("Error fething documents: \(error)")
} else {
guard let snap = snapshot else { return }
for document in snap.documents {
let data = document.data()
var chatMessages: [Message] = []
var chat: ChatModel = ChatModel(person: Person(name: "", imgString: ""), messages: [], hasUnreadMessage: false, isActive: false)
let personRef = (document.get("person") as? DocumentReference ?? nil)?.getDocument(completion: { personSnapshot, personErr in
if let personError = personErr {
debugPrint("Error getting chat person: \(personError)")
} else {
guard let personSnap = personSnapshot else { return }
let personData = personSnap.data()
chat.person.imgString = personData?["imgString"] as? String ?? ""
chat.person.name = personData?["name"] as? String ?? ""
}
})
let messages = document.get("messages") as? [DocumentReference] ?? []
for message in messages {
message.getDocument { messageSnapshot, messageErr in
if let messageError = messageErr {
debugPrint("Error getting message: \(messageError)")
} else {
guard let messageSnap = messageSnapshot else { return }
let messageData = messageSnap.data()
let date = messageData?["date"] as? String ?? ""
let text = messageData?["text"] as? String ?? ""
let type = messageData?["type"] as? String ?? ""
chatMessages.append(Message(text, type: type, date: date))
}
}
}
chat.hasUnreadMessage = data["hasUnreadMessage"] as? Bool ?? false
chat.isActive = data["isActive"] as? Bool ?? false
chat.messages = chatMessages
self.chats.append(chat)
}
}
}
}
Edit:
Here are the outputs. I saw that I called the imgString print before the ChatModel print and they appeared in the opposite order
BT_Tech.ChatModel(_id: FirebaseFirestoreSwift.DocumentID<Swift.String>(value: Optional("D195D8D8-F401-4C71-B571-4877E6574B68")), person: BT_Tech.Person(_id: FirebaseFirestoreSwift.DocumentID<Swift.String>(value: Optional("46768262-EB88-42B3-A9FF-4F9FF3A7B7F1")), name: "", imgString: ""), messages: [], hasUnreadMessage: true, isActive: true)
"imgString: girl1"
Here is the ChatModel
struct ChatModel: Identifiable, Codable {
#DocumentID var id: String? = UUID().uuidString
var person: Person
var messages: [Message]
var hasUnreadMessage: Bool
var isActive: Bool
}
struct Person: Identifiable, Codable {
#DocumentID var id: String? = UUID().uuidString
var name: String
var imgString: String
}
struct Message: Identifiable, Codable {
#DocumentID var id: String? = UUID().uuidString
var date: String
var text: String
var type: String
init(_ text: String, type: String, date: String) {
self.text = text
self.type = type
self.date = date
}
init(_ text: String, type: String) {
self.init(text, type: type, date: "")
}
}

Unpacking Firestore array with objects to a model in swift

I have a project in swift with Firestore for the database. My firestore dataset of a user looks like this. User details with an array that contains objects.
I have a function that gets the specifick user with all firestore data:
func fetchUser(){
db.collection("users").document(currentUser!.uid)
.getDocument { (snapshot, error ) in
do {
if let document = snapshot {
let id = document.documentID
let firstName = document.get("firstName") as? String ?? ""
let secondName = document.get("secondName") as? String ?? ""
let imageUrl = document.get("imageUrl") as? String ?? ""
let joinedDate = document.get("joinedDate") as? String ?? ""
let coins = document.get("coins") as? Int ?? 0
let challenges = document.get("activeChallenges") as? [Challenge] ?? []
let imageLink = URL(string: imageUrl)
let imageData = try? Data(contentsOf: imageLink!)
let image = UIImage(data: imageData!) as UIImage?
let arrayWithNoOptionals = document.get("activeChallenges").flatMap { $0 }
print("array without opt", arrayWithNoOptionals)
self.user = Account(id: id, firstName: firstName, secondName: secondName, email: "", password: "", profileImage: image ?? UIImage(), joinedDate: joinedDate, coins: coins, activeChallenges: challenges)
}
else {
print("Document does not exist")
}
}
catch {
fatalError()
}
}
}
This is what the user model looks like:
class Account {
var id: String?
var firstName: String?
var secondName: String?
var email: String?
var password: String?
var profileImage: UIImage?
var coins: Int?
var joinedDate: String?
var activeChallenges: [Challenge]?
init(id: String, firstName: String,secondName: String, email: String, password: String, profileImage: UIImage, joinedDate: String, coins: Int, activeChallenges: [Challenge]) {
self.id = id
self.firstName = firstName
self.secondName = secondName
self.email = email
self.password = password
self.profileImage = profileImage
self.joinedDate = joinedDate
self.coins = coins
self.activeChallenges = activeChallenges
}
init() {
}
}
The problem is I don't understand how to map the activeChallenges from firestore to the array of the model. When I try : let challenges = document.get("activeChallenges") as? [Challenge] ?? []
The print contains an empty array, but when i do: let arrayWithNoOptionals = document.get("activeChallenges").flatMap { $0 } print("array without opt", arrayWithNoOptionals)
This is the output of the flatmap:
it returns an optional array
System can not know that activeChallenges is array of Challenge object. So, you need to cast it to key-value type (Dictionary) first, then map it to Challenge object
let challengesDict = document.get("activeChallenges") as? [Dictionary<String: Any>] ?? [[:]]
let challenges = challengesDict.map { challengeDict in
let challenge = Challenge()
challenge.challengeId = challengeDict["challengeId"] as? String
...
return challenge
}
This is the same way that you cast snapshot(document) to Account object

getting Thread 1: EXC_BAD_ACCESS (code=1, address=0x11) on realm query in swift

i have two models (User and Project) as below:
class User: Object { // Users that logged in app in specific device atleast once
#objc dynamic var serverId: Int = 0
#objc dynamic var firstName: String = ""
#objc dynamic var lastName: String = ""
#objc dynamic var email: String?
#objc dynamic var company: String?
#objc dynamic var phoneNumber: String = ""
#objc dynamic var syncBaseTime: String = ""
let projects = LinkingObjects(fromType: Project.self, property: "user")
convenience init(_serverId: Int, _firstName: String, _lastName: String, _phoneNumber: String) {
self.init()
self.serverId = _serverId
self.firstName = _firstName
self.lastName = _lastName
self.phoneNumber = _phoneNumber
}
override static func primaryKey() -> String? {
return "serverId"
}
}
class Project: Object {
#objc dynamic var serverId: Int = 0
#objc dynamic var name: String = ""
#objc dynamic var stateId: Int = 0
#objc dynamic var stateName: String = ""
#objc dynamic var cityId: Int = 0
#objc dynamic var cityName: String = ""
#objc dynamic var user: User?
#objc dynamic var isOwner: Bool = false
#objc dynamic var isActive: Bool = false
#objc dynamic var compoundKey: String = ""
#objc dynamic var syncDetailTime: String = ""
let accountTitles = LinkingObjects(fromType: AccountTitle.self, property: "project")
let notes = LinkingObjects(fromType: Note.self, property: "project")
convenience init(_serverId: Int, _name: String, _stateId: Int, _stateName: String,
_cityId: Int, _cityName: String, _user: User, _isOwner: Bool, _isActive: Bool) {
self.init()
self.serverId = _serverId
self.name = _name
self.stateId = _stateId
self.stateName = _stateName
self.cityId = _cityId
self.cityName = _cityName
self.user = _user
self.isOwner = _isOwner
self.isActive = _isActive
self.compoundKey = "\(self.user!.serverId)-\(self.serverId)"
}
override static func primaryKey() -> String? {
return "compoundKey"
}
}
the problem occurs when I want to execute this query on them and get projects that current user working on them, but they are not currently choosen:
self.realm.objects(Project.self).filter(NSPredicate(format: "user.serverId == %# && isActive == true && serverId != %#", self.currentUser.serverId, self.currentProject.serverId))
I get this error with no more information from Xcode:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x11)
and I could not find where is my mistake and I will appreciate any help with this.
You are using the wrong specifier in the format string, %# is only for objects, and Int is not an object in terms of NSPredicate
Use %ld for Int
self.realm.objects(Project.self).filter(NSPredicate(format: "user.serverId == %ld && isActive == true && serverId != %ld", self.currentUser.serverId, self.currentProject.serverId))
Note: This is Swift. Please don't use ugly objective-c-ish variable names with starting underscore characters. self.name = name does compile.

Swift Firebase chat room

I have chat app with possibility send messages one to one (fromId/toId). I want to upgrade it with chat rooms. How i can do that? What DB structure do i need for ChatingRoom? What else i need to do that?
My User.swift model:
import Foundation
import Firebase
class User: NSObject {
var id: String?
var name: String?
var login: String?
var email: String?
var profileImageUrl: String?
var role: String?
var isOnline: String?
init(dictionary: [String: AnyObject]) {
self.isOnline = dictionary["isOnline"] as? String
self.id = dictionary["userID"] as? String
self.name = dictionary["name"] as? String
self.login = dictionary["username"] as? String
self.email = dictionary["email"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
self.role = dictionary["role"] as? String
}
}
Message.swift model:
import UIKit
import Firebase
class Message: NSObject {
var fromId: String?
var text: String?
var timestamp: NSNumber?
var toId: String?
var imageUrl: String?
var videoUrl: String?
var imageWidth: NSNumber?
var imageHeight: NSNumber?
init(dictionary: [String: Any]) {
self.fromId = dictionary["fromId"] as? String
self.text = dictionary["text"] as? String
self.toId = dictionary["toId"] as? String
self.timestamp = dictionary["timestamp"] as? NSNumber
self.imageUrl = dictionary["imageUrl"] as? String
self.videoUrl = dictionary["videoUrl"] as? String
self.imageWidth = dictionary["imageWidth"] as? NSNumber
self.imageHeight = dictionary["imageHeight"] as? NSNumber
}
func chatPartnerId() -> String? {
return fromId == Auth.auth().currentUser?.uid ? toId : fromId
}
}
Well currently you have From / To.
So To is going to be a room rather than a person.
Users will need to be able to join a room (or rooms) in order to see the messages that are sent to that room.
So your need a Rooms node.
If you have a messages node then you can just sort by To (room) instead of To (user) to get all the messages sent in that chat room. From will always be the User that wrote the message

want to fetch userdata from firebase into a dictionary

I hope you could help me:
I try to fetch userdata from a firebase database into a user class and call "setValuesForKeys". I do understand that my class properties have to be exactly the same as in the firebase dictionary, but I've got the error "this class is not key value coding-compliant for the key city."
func fetchUsers(){
Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let usersDictionary = snapshot.value as? [String: String] {
let users = Userdata()
users.setValuesForKeys(usersDictionary)
}
}, withCancel: nil)
}
And my user class is
class Userdata: NSObject {
var email: String?
var password: String?
var firstname: String?
var lastname: String?
var street: String?
var streetno: String?
var zipcode: String?
var city: String?
var phone: String? }
The snapshot from firebase looks like
Snap (ndLBXXX75Oe9Y1PXrqfISL8A4v82) {
city = Washington;
email = "1#a.com";
firstname = Andre;
lastname = Doe;
password = xxxxxx;
phone = "";
street = "Mainstreet";
streetno = 1;
zipcode = 11111;
}
And the dictionary from the database looks like
["city": Washington, "firstname": Andre, "lastname": Doe, "email": 1#a.com, "password": xxxxxx, "streetno": 1, "phone": , "street": Mainstreet, "zipcode": 11111]
I have a solution so far by using:
users.city = dictionary["city"]
My question / problem: I do want to understand the problem behind the error message "this class is not key value coding-compliant for the key city." because the key at the class and in the firebase snapshot looks like the same.
Working solution: I had to extend my user class. Now, the whole user class looks like:
Here is the code :
import Foundation
class UserData: NSObject {
var id: String?
var email: String?
var password: String?
var salutation: String?
var degree: String?
var firstname: String?
var lastname: String?
var street: String?
var streetno: String?
var zipcode: String?
var city: String?
var phone: String?
var profileImage: String?
init(dictionary: [String: Any]) {
self.city = dictionary["city"] as? String
self.id = dictionary["id"] as? String
self.email = dictionary["email"] as? String
self.salutation = dictionary["salutation"] as? String
self.degree = dictionary["degree"] as? String
self.firstname = dictionary["firstname"] as? String
self.lastname = dictionary["lastname"] as? String
self.password = dictionary["password"] as? String
self.phone = dictionary["phone"] as? String
self.street = dictionary["street"] as? String
self.streetno = dictionary["streetno"] as? String
self.zipcode = dictionary["zipcode"] as? String
self.profileImage = dictionary["profileImage"] as? String
}
}