Comments (9)
You can keep a reference to the MutableDataSource
so that you can call deleteItem(at:)
when needed.
You could also have two separate DataSource
that you bind to the ProxyDataSource
depending on the state of your toggle button, like so :
dataSource.innerDataSource <~ toggled.map { $0 ? someDataSource : someOtherDataSource }
from datasource.
You might be creating a retain cycle by capturing self
there.
What you can do instead is have a private let userDataSource: MutableProperty<MutableDataSource<Any>>
, then bind userDataSource <~ user.map { ... }
(there will be no need to capture self
in that closure), and then bind proxyDataSource.innerDataSource <~ userDataSource
.
That might be even nicer to wrap that MutableDataSource
into a UserViewModel
and move some of the code there.
from datasource.
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 MutableDataSource
called mutableDataSource
.
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 MutableDataSource
by passing in the items in it's init method and assigned it like this mutableDataSource = MutableDataSource(items)
. This fixes my initial problem of editing the datasource.
But the issue with this assigning approach is that, the changes to the user
object's values are not reflected on the tableview.
How should I bind the user object like this self.dataSource.innerDataSource <~ user.map { user in ... }
to the datasource instead of assigning it?
Doing this mutableDataSource <~ user.map { user in
throws the following error.
Binary operator '<~' cannot be applied to operands of type 'MutableDataSource' and 'SignalProducer<(), NoError>'
Sorry abut the trivial questions. I'm quite new to ReactiveSwift so I'm still learning the ropes 😬
from datasource.
Indeed in your code above you are just changing the reference to the MutableDataSource
, and this change cannot be propagated through ReactiveSwift
Signal/Property
system.
If you want to swap the DataSource
when the user changes, you need to wrap it in a MutableProperty
at some point, and bind the innerDataSource
of that ProxyDataSource
to this MutableProperty
.
from datasource.
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 mutableDataSource
. Is there a way to do that?
from datasource.
The above would still apply.
from datasource.
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 mutableDataSource
property as well as the dataSource
property from the original code as it is. And I assign the items to mutableDataSource
inside the innerDataSource
binding code.
Thanks a lot for your help!
from datasource.
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.
Constant 'self.userDataSource' used before being initialized
I'm not sure why.
Side question: I'm accessing the mutable datasource like this userDataSource.value.deleteItem
to make changes to it. I hope that's the proper way?
from datasource.
Hi again, I still need help on the last snag 😬
from datasource.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datasource.