How to pre-populate data in Swift?

E

enigrify

Guest
I'm having an issue trying to store a bunch of items with properties in my app. What I have already tried (and mostly works) is making my custom class:

class Waterz {

var entitle: String? = nil
var artitle: String? = nil
var isFavorited: Bool? = nil
var date: [String]? = nil
var frequency: [daysOfWeek]? = nil
var subMenus: Array<Waterz>? = nil
var content: String? = nil
var location: String? = nil
var audio: [(String, reciterNames)]? = nil

init() {
}

convenience init(entitle: String, artitle: String, isFavorited: Bool?, date: [String]?, frequency: [daysOfWeek]?, content: String?, location: String?, audio: [(String, reciterNames)]?) {
self.init()
...
}

}


and then I made another class where I have my database:

class WaterzBank {

// all
var allItems: [Waterz]
var allDuas: [Waterz]
var allAmal: [Waterz]

// main categories
var mainCategories: Waterz

// individual categories
var duaKumayl: Waterz
var duaMakarem: Waterz
var duaNudba: Waterz

... (like 5 thousand more)
}

// Initialize them here
init() {
duaKumayl = Waterz(entitle: "Dua Kumayl", artitle: "دعاء كميل", isFavorited: false, date: ["1-27"], frequency: [.sunday], content: "kumayl", location: nil, audio: nil)
duaMakarem = Waterz(entitle: "Dua Makarem Al-Akhlaaq", artitle: "دعاء مكارم الأخلاق", isFavorited: true, date: ["1-27"], frequency: [.sunday, .everyday], content: "duaMakaremAlAkhlaaq", location: nil, audio: nil)
duaNudba = Waterz(entitle: "Dua Nudba", artitle: "دعاء النّدبة", isFavorited: true, date: nil, frequency: nil, content: "duaNudba", location: nil, audio: nil)

...(initializing all the other variables)

favorites = Waterz(entitle: "Favorites", artitle: "المفضلة", subMenus: allItems.filter {
$0.isFavorited == true
})


// MARK: - Main

mainCategories = Waterz(entitle: "Categories", artitle: "الأقسام", subMenus: [duas, amal, ziyarat, munajat, taqeebat, others, favorites])
}


Everything works with this setup, except permanently changing the status of the items being favorited (by the user, everything starts off as unfavorited, but I want them to be able to change that). I tried so many ways such as using direct Realm, JSON parsing to Realm, CoreData, UserDefaults, but none of it worked out for me.

So everything starts off as unfavorited. When the user tries to favorite one of them. It does work, but when they close the app and reopen it, it reverts to unfavorited.

The only thing the user should be able to change is the 'isFavorited' property. Everything else, I set and it comes with the app as soon as you download it.

What should I do in this case? Do I have to completely change my system? Is there a way to parse all of my initialized variables into a Realm Database? If so, will I be able to parse things like audio, which is of type [(String, Enum.type)]? This is my first app and I'm really confused. Thank you for your help.

Continue reading...
 
Top