-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about making changes to the inner datasource which is a MutableDataSource #20
Comments
You can keep a reference to the You could also have two separate
|
Hi, thanks for the quick response. I was actually trying out something similar to your first suggestion as well. I declared a class level variable for final class ProfileInfoCardViewModel {
let profileViewModel: ProfileViewModel
var mutableDataSource = MutableDataSource<Any>()
var dataSource: DataSource {
return mutableDataSource
}
init(profileViewModel: ProfileViewModel) {
self.profileViewModel = profileViewModel
let user = profileViewModel.user.producer.skipNil()
if let user = profileViewModel.user.value {
if user.isLoggedInUser {
if user.userLikes ?? 0 > 0 {
items.append(ProfileInfoRatingCellViewModel(userLikes : user.userLikes ?? 0))
} else {
items.append(ProfileInfoNoRatingsYetCellViewModel())
}
items.append(ProfileInfoUpdateCellViewModel())
}
mutableDataSource = MutableDataSource(items)
}
}
func userTappedToggleButton(_ show: Bool, index: Int) {
if show {
mutableDataSource.insertItem(ProfileLevelIndexCellViewModel(), at: index)
} else {
mutableDataSource.deleteItem(at: index)
}
}
} I initialized the But the issue with this assigning approach is that, the changes to the How should I bind the user object like this Doing this
Sorry abut the trivial questions. I'm quite new to ReactiveSwift so I'm still learning the ropes 😬 |
Indeed in your code above you are just changing the reference to the |
No no, the user itself doesn't change :) Only the user's property values may change. The tableview should only reflect those changes. So no need for multiple datasources and swapping them. I'm only trying to bind the one user object to the |
The above would still apply. |
I think I managed to get it working! final class ProfileInfoCardViewModel {
let dataSource: DataSource
let profileViewModel: ProfileViewModel
var mutableDataSource = MutableDataSource<Any>()
init(profileViewModel: ProfileViewModel) {
self.profileViewModel = profileViewModel
let user = profileViewModel.user.producer.skipNil()
let proxyDataSource = ProxyDataSource()
self.dataSource = proxyDataSource
proxyDataSource.innerDataSource <~ user.map { user in
if user.isLoggedInUser {
if user.userLikes ?? 0 > 0 {
items.append(ProfileInfoRatingCellViewModel(userLikes : user.userLikes ?? 0))
} else {
items.append(ProfileInfoNoRatingsYetCellViewModel())
}
items.append(ProfileInfoUpdateCellViewModel())
}
self.mutableDataSource = MutableDataSource(items)
return self.mutableDataSource
}
}
func userTappedToggleButton(_ show: Bool, index: Int) {
if show {
mutableDataSource.insertItem(ProfileLevelIndexCellViewModel(), at: index)
} else {
mutableDataSource.deleteItem(at: index)
}
}
} I kept the Thanks a lot for your help! |
You might be creating a retain cycle by capturing |
Hi @Vadim-Yelagin, thanks for the response. Understood. I modified my code. final class ProfileInfoCardViewModel {
let dataSource: DataSource
let profileViewModel: ProfileViewModel
private let userDataSource: MutableProperty<MutableDataSource<Any>>
init(profileViewModel: ProfileViewModel) {
self.profileViewModel = profileViewModel
let user = profileViewModel.user.producer.skipNil()
userDataSource <~ user.map { user in
if user.isLoggedInUser {
if user.isLoggedInUser {
if user.userLikes ?? 0 > 0 {
items.append(ProfileInfoRatingCellViewModel(userLikes : user.userLikes ?? 0))
} else {
items.append(ProfileInfoNoRatingsYetCellViewModel())
}
items.append(ProfileInfoUpdateCellViewModel())
}
return MutableDataSource(items)
}
let proxyDataSource = ProxyDataSource()
proxyDataSource.innerDataSource <~ userDataSource.map { $0 }
self.dataSource = proxyDataSource
}
func userTappedToggleButton(_ show: Bool, index: Int) {
if show {
userDataSource.value.insertItem(ProfileLevelIndexCellViewModel(), at: index)
} else {
userDataSource.value.deleteItem(at: index)
}
}
}
One small issue. I'm currently getting the following error.
I'm not sure why. Side question: I'm accessing the mutable datasource like this |
Hi again, I still need help on the last snag 😬 |
There is a simple static tableview where it shows profile data. Since this data was not changing, it has used the
StaticDataSource
.There is a new requirement to make this tableview dynamic. Upon a tap of a button, I need to insert/remove a cell from this tableview. So I opted to use the
MutableDataSource
. Also I changed thedataSource
class variable fromDataSource
type toProxyDataSource
directly.The data loads fine still. However I'm having trouble figuring out how to make changes to the inner data source (add/remove an item).
The text was updated successfully, but these errors were encountered: