multiple map with rxswift












0















I am trying to create sections for a tableview from an array of item on which each array item is a sections item.




My quesiton is how can I make cart.map return an array of

CartProductSection ([CartProductSection]) instead of assign it to a variable. Thanks.




My code
let cart: BehaviorRelay
struct Output {
let items: Observable<[CartProductSection]>
}



           var sectionPerSupplierItems: [CartProductSection] = 

_ = cart.map { (cart) in
let cartItemsPerSupplier = self.sortModelItemPerSection(items: cart.items)

print(cart.items.count)
_ = cartItemsPerSupplier.map { (cartItemsPerSupplier) in

var items: [CartProductSectionItem] =
for cartItem in cartItemsPerSupplier.cartItems {
items.append(CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: cartItem)))
}

sectionPerSupplierItems.append(CartProductSection.cartItemModel
(title: cartItemsPerSupplier.companyName, items: items))
}
}

return Output(items: Observable.just(sectionPerSupplierItems))









share|improve this question

























  • There's not enough context to go on. What is the type of cart? Are you using RxDataSources? What type are you expecting to feed into the table view?

    – Daniel T.
    Dec 31 '18 at 14:01











  • code updated, yes am using rxdatasrouce.

    – Mohamed ALOUANE
    Dec 31 '18 at 14:10
















0















I am trying to create sections for a tableview from an array of item on which each array item is a sections item.




My quesiton is how can I make cart.map return an array of

CartProductSection ([CartProductSection]) instead of assign it to a variable. Thanks.




My code
let cart: BehaviorRelay
struct Output {
let items: Observable<[CartProductSection]>
}



           var sectionPerSupplierItems: [CartProductSection] = 

_ = cart.map { (cart) in
let cartItemsPerSupplier = self.sortModelItemPerSection(items: cart.items)

print(cart.items.count)
_ = cartItemsPerSupplier.map { (cartItemsPerSupplier) in

var items: [CartProductSectionItem] =
for cartItem in cartItemsPerSupplier.cartItems {
items.append(CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: cartItem)))
}

sectionPerSupplierItems.append(CartProductSection.cartItemModel
(title: cartItemsPerSupplier.companyName, items: items))
}
}

return Output(items: Observable.just(sectionPerSupplierItems))









share|improve this question

























  • There's not enough context to go on. What is the type of cart? Are you using RxDataSources? What type are you expecting to feed into the table view?

    – Daniel T.
    Dec 31 '18 at 14:01











  • code updated, yes am using rxdatasrouce.

    – Mohamed ALOUANE
    Dec 31 '18 at 14:10














0












0








0








I am trying to create sections for a tableview from an array of item on which each array item is a sections item.




My quesiton is how can I make cart.map return an array of

CartProductSection ([CartProductSection]) instead of assign it to a variable. Thanks.




My code
let cart: BehaviorRelay
struct Output {
let items: Observable<[CartProductSection]>
}



           var sectionPerSupplierItems: [CartProductSection] = 

_ = cart.map { (cart) in
let cartItemsPerSupplier = self.sortModelItemPerSection(items: cart.items)

print(cart.items.count)
_ = cartItemsPerSupplier.map { (cartItemsPerSupplier) in

var items: [CartProductSectionItem] =
for cartItem in cartItemsPerSupplier.cartItems {
items.append(CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: cartItem)))
}

sectionPerSupplierItems.append(CartProductSection.cartItemModel
(title: cartItemsPerSupplier.companyName, items: items))
}
}

return Output(items: Observable.just(sectionPerSupplierItems))









share|improve this question
















I am trying to create sections for a tableview from an array of item on which each array item is a sections item.




My quesiton is how can I make cart.map return an array of

CartProductSection ([CartProductSection]) instead of assign it to a variable. Thanks.




My code
let cart: BehaviorRelay
struct Output {
let items: Observable<[CartProductSection]>
}



           var sectionPerSupplierItems: [CartProductSection] = 

_ = cart.map { (cart) in
let cartItemsPerSupplier = self.sortModelItemPerSection(items: cart.items)

print(cart.items.count)
_ = cartItemsPerSupplier.map { (cartItemsPerSupplier) in

var items: [CartProductSectionItem] =
for cartItem in cartItemsPerSupplier.cartItems {
items.append(CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: cartItem)))
}

sectionPerSupplierItems.append(CartProductSection.cartItemModel
(title: cartItemsPerSupplier.companyName, items: items))
}
}

return Output(items: Observable.just(sectionPerSupplierItems))






ios swift reactive-programming rx-swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 14:10







Mohamed ALOUANE

















asked Dec 31 '18 at 12:52









Mohamed ALOUANEMohamed ALOUANE

2,45721740




2,45721740













  • There's not enough context to go on. What is the type of cart? Are you using RxDataSources? What type are you expecting to feed into the table view?

    – Daniel T.
    Dec 31 '18 at 14:01











  • code updated, yes am using rxdatasrouce.

    – Mohamed ALOUANE
    Dec 31 '18 at 14:10



















  • There's not enough context to go on. What is the type of cart? Are you using RxDataSources? What type are you expecting to feed into the table view?

    – Daniel T.
    Dec 31 '18 at 14:01











  • code updated, yes am using rxdatasrouce.

    – Mohamed ALOUANE
    Dec 31 '18 at 14:10

















There's not enough context to go on. What is the type of cart? Are you using RxDataSources? What type are you expecting to feed into the table view?

– Daniel T.
Dec 31 '18 at 14:01





There's not enough context to go on. What is the type of cart? Are you using RxDataSources? What type are you expecting to feed into the table view?

– Daniel T.
Dec 31 '18 at 14:01













code updated, yes am using rxdatasrouce.

– Mohamed ALOUANE
Dec 31 '18 at 14:10





code updated, yes am using rxdatasrouce.

– Mohamed ALOUANE
Dec 31 '18 at 14:10












1 Answer
1






active

oldest

votes


















1














I would likely write the code like this (I'm still making a bunch of assumptions here.):



let items = cart
.map { [unowned self] in self.sortModelItemPerSection(items: $0.items) }
.map { cartItemsPerSuppliers in
cartItemsPerSuppliers.map { CartProductSection.cartItemModel(from: $0) }
}
return Output(items: items)


The above assumes something like this exists:



extension CartProductSection {
static func cartItemModel(from supplier: CartItemsPerSupplier) -> CartProductSection {
return cartItemModel(title: supplier.companyName, items: supplier.cartItems.map { CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: $0)) })
}
}





share|improve this answer
























  • Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

    – Daniel T.
    Jan 1 at 2:32











  • Thank you! I got to learn new stuff from this :). Happy new year!

    – Mohamed ALOUANE
    Jan 1 at 13:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987750%2fmultiple-map-with-rxswift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I would likely write the code like this (I'm still making a bunch of assumptions here.):



let items = cart
.map { [unowned self] in self.sortModelItemPerSection(items: $0.items) }
.map { cartItemsPerSuppliers in
cartItemsPerSuppliers.map { CartProductSection.cartItemModel(from: $0) }
}
return Output(items: items)


The above assumes something like this exists:



extension CartProductSection {
static func cartItemModel(from supplier: CartItemsPerSupplier) -> CartProductSection {
return cartItemModel(title: supplier.companyName, items: supplier.cartItems.map { CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: $0)) })
}
}





share|improve this answer
























  • Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

    – Daniel T.
    Jan 1 at 2:32











  • Thank you! I got to learn new stuff from this :). Happy new year!

    – Mohamed ALOUANE
    Jan 1 at 13:33
















1














I would likely write the code like this (I'm still making a bunch of assumptions here.):



let items = cart
.map { [unowned self] in self.sortModelItemPerSection(items: $0.items) }
.map { cartItemsPerSuppliers in
cartItemsPerSuppliers.map { CartProductSection.cartItemModel(from: $0) }
}
return Output(items: items)


The above assumes something like this exists:



extension CartProductSection {
static func cartItemModel(from supplier: CartItemsPerSupplier) -> CartProductSection {
return cartItemModel(title: supplier.companyName, items: supplier.cartItems.map { CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: $0)) })
}
}





share|improve this answer
























  • Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

    – Daniel T.
    Jan 1 at 2:32











  • Thank you! I got to learn new stuff from this :). Happy new year!

    – Mohamed ALOUANE
    Jan 1 at 13:33














1












1








1







I would likely write the code like this (I'm still making a bunch of assumptions here.):



let items = cart
.map { [unowned self] in self.sortModelItemPerSection(items: $0.items) }
.map { cartItemsPerSuppliers in
cartItemsPerSuppliers.map { CartProductSection.cartItemModel(from: $0) }
}
return Output(items: items)


The above assumes something like this exists:



extension CartProductSection {
static func cartItemModel(from supplier: CartItemsPerSupplier) -> CartProductSection {
return cartItemModel(title: supplier.companyName, items: supplier.cartItems.map { CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: $0)) })
}
}





share|improve this answer













I would likely write the code like this (I'm still making a bunch of assumptions here.):



let items = cart
.map { [unowned self] in self.sortModelItemPerSection(items: $0.items) }
.map { cartItemsPerSuppliers in
cartItemsPerSuppliers.map { CartProductSection.cartItemModel(from: $0) }
}
return Output(items: items)


The above assumes something like this exists:



extension CartProductSection {
static func cartItemModel(from supplier: CartItemsPerSupplier) -> CartProductSection {
return cartItemModel(title: supplier.companyName, items: supplier.cartItems.map { CartProductSectionItem.cartItem(viewModel: CartItemViewModel(with: $0)) })
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 2:03









Daniel T.Daniel T.

13.7k22734




13.7k22734













  • Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

    – Daniel T.
    Jan 1 at 2:32











  • Thank you! I got to learn new stuff from this :). Happy new year!

    – Mohamed ALOUANE
    Jan 1 at 13:33



















  • Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

    – Daniel T.
    Jan 1 at 2:32











  • Thank you! I got to learn new stuff from this :). Happy new year!

    – Mohamed ALOUANE
    Jan 1 at 13:33

















Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

– Daniel T.
Jan 1 at 2:32





Also, if I were writing this, I would move sortModelItemPerSection(items:) out of the class so I didn't have to reference self.

– Daniel T.
Jan 1 at 2:32













Thank you! I got to learn new stuff from this :). Happy new year!

– Mohamed ALOUANE
Jan 1 at 13:33





Thank you! I got to learn new stuff from this :). Happy new year!

– Mohamed ALOUANE
Jan 1 at 13:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987750%2fmultiple-map-with-rxswift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas