Re: Using NSFetchedResultsController with many-to-many relationship

2018-03-10 Thread Glen Huang
Works like a charm. Thanks!

> On 11 Mar 2018, at 6:03 AM, Quincey Morris 
>  wrote:
> 
> On Mar 10, 2018, at 13:06 , Steve Christensen  wrote:
>> 
>> Don't complicate your life by managing multiple NSFetchedResultsControllers. 
>> Just create a single one that returns Club entities.
> 
> The other valid approach, I think, is to “join” the two entities. That is, 
> introduce a third entity, say “Membership” which represents a single Person 
> belonging to a single Club. Person then becomes one-to-many for Membership, 
> and Club becomes one-to-many for Membership, and there is no many-to-many 
> relationship any more.
> 
> There is still complication in doing the kind of enumerations needed for a 
> table view, but life is generally easier without many-to-many relationships.

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Using NSFetchedResultsController with many-to-many relationship

2018-03-10 Thread Quincey Morris
On Mar 10, 2018, at 13:06 , Steve Christensen  wrote:
> 
> Don't complicate your life by managing multiple NSFetchedResultsControllers. 
> Just create a single one that returns Club entities.

The other valid approach, I think, is to “join” the two entities. That is, 
introduce a third entity, say “Membership” which represents a single Person 
belonging to a single Club. Person then becomes one-to-many for Membership, and 
Club becomes one-to-many for Membership, and there is no many-to-many 
relationship any more.

There is still complication in doing the kind of enumerations needed for a 
table view, but life is generally easier without many-to-many relationships.
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Using NSFetchedResultsController with many-to-many relationship

2018-03-10 Thread Steve Christensen
Don't complicate your life by managing multiple NSFetchedResultsControllers. 
Just create a single one that returns Club entities. For your table view data 
source methods:

func numberOfSections(in tableView: UITableView) -> Int
{
return frc.fetchedObjects.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> 
Int
{
let club = frc.fetchedObjects[section] as! Club

return club.persons.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
UITableViewCell
{
let club = frc.fetchedObjects[indexPath.section] as! Club
let person = club.person[indexPath.row]

return self.cell(forPerson: person)
}

If your Club and Person entities will remain static while the table view is 
visible then you can just do the fetch once in viewDidLoad() and use the info 
you get back as-is. If your app supports some kind of async changes to your 
model data that affect which Clubs exist then you should implement the 
NSFetchedResultController delegate methods so that you know when those changes 
occur.

To watch for Person changes, you could set up KVO observing of the properties 
you're interested in. When observeValue(...) is called then reverse look-up the 
clubs of which the person is a member and reload cells corresponding to that 
person in each of its clubs:

...
if let person = object as! Person
{
var cellsToReload = [IndexPath]()

for club in person.clubs
{
if let section = frc.fetchedObjects.index(of: club),
   let row = club.persons.index(of: person)
{
let indexPath = IndexPath(row: row, section: section)

cellsToReload.append(indexPath)
}
}

if cellsToReload.count > 0
{
tableView.performBatchUpdates(
{
tableView.reloadRows(at: cellsToReload, with: .automatic)
},
completion: nil)
}
}
...


Steve


> On Mar 10, 2018, at 12:47 AM, Glen Huang  wrote:
> 
> Hi,
> 
> I have two models: Person and Club. They have a many-to-many relationship. I 
> want to display all people in a table view sectioned by clubs (which means 
> there might be duplicate people across sections). I wonder how to do that 
> with NSFetchedResultsController? NSFetchRequest never returns the same object 
> more than once. 
> 
> One solution I can come up with is to use multiple 
> NSFetchedResultsController, one for each section(club). But in my case people 
> are editable, so I’d like to also track changes. Listen to multiple 
> NSFetchedResultsController seems pretty cumbersome if not impossible 
> (especially with regard to the adding or removing of clubs, either because 
> everyone leaves a club or a person joins a new club).
> 
> I wonder if there is a direct way to achieve that or a better way to 
> approximate? 
> 
> Best
> Glen

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Using NSFetchedResultsController with many-to-many relationship

2018-03-10 Thread Glen Huang
Hi,

I have two models: Person and Club. They have a many-to-many relationship. I 
want to display all people in a table view sectioned by clubs (which means 
there might be duplicate people across sections). I wonder how to do that with 
NSFetchedResultsController? NSFetchRequest never returns the same object more 
than once. 

One solution I can come up with is to use multiple NSFetchedResultsController, 
one for each section(club). But in my case people are editable, so I’d like to 
also track changes. Listen to multiple NSFetchedResultsController seems pretty 
cumbersome if not impossible (especially with regard to the adding or removing 
of clubs, either because everyone leaves a club or a person joins a new club).

I wonder if there is a direct way to achieve that or a better way to 
approximate? 

Best
Glen
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Put UICollectionView within UITableViewCell

2018-03-10 Thread Glen Huang
I’ll give it a shot, thank you very much

> On 10 Mar 2018, at 10:27 AM, Cosmo Birch  wrote:
> 
> But you presumably know the width (i.e. the CollectionView width minus any 
> insets), and you can calculate the height based on that with NSString or 
> NSAttributedString boundingRect functions. Assuming you have a subclass for 
> your CollectionViewCell, you can add a method to return your cell height 
> based on its contents layout.
> 
>> On Mar 9, 2018, at 8:21 AM, Gary L. Wade  
>> wrote:
>> 
>> @Cosmo
>> 
>> My collection items contain label, so I don’t know the exact sizes before 
>> hand.
>> 
> 
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/heyhgl%40gmail.com
> 
> This email sent to hey...@gmail.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com