If the entities that are being deleted are not loaded into memory, there is no need to update your application after the NSBatchDeleteRequest has been executed. However, if you are deleting objects in the persistence layer and those entities are also in memory, it is important that you notify the application that the objects in memory are stale and need to be refreshed.
To do this, first make sure the resultType of the NSBatchDeleteRequest is set to NSBatchDeleteRequestResultType.resultTypeObjectIDs before the request is executed. When the request has completed successfully, the resulting NSPersistentStoreResult instance that is returned will have an array of NSManagedObjectID instances referenced in the result property. That array of NSManagedObjectID instances can then be used to update one or more NSManagedObjectContext instances.
This method runs a modal event loop for the specified window synchronously. It displays the specified window, makes it key, starts the run loop, and processes events for that window. (You do not need to show the window yourself.) While the app is in that loop, it does not respond to any other events (including mouse, keyboard, or window-close events) unless they are associated with the window. It also does not perform any tasks (such as firing timers) that are not associated with the modal run loop. In other words, this method consumes only enough CPU time to process events and dispatch them to the action methods associated with the modal window.
Set NSVisualEffectView as contentView of NSWindow, and our main view as subview of it. Remember to set frame or autoresizing mask as non-direct content view does not get full size as the window
structMainView: View{ @EnvironmentObjectvar store: Store
var body: some View { List { ForEach(store.books.enumerated().map({ $0 }), id: \.element.id) { index, book in { Text(book.name) .onTapGesture { self.store.selectedIndex = index } } }
I see that the modifier needs to do something on the content, otherwise it is not getting called! This logs on the modifier, when the View is created. A View won’t be recreated unless necessary
1 2 3 4 5 6 7 8 9 10 11 12 13 14
structLogModifier: ViewModifier{ let text: String funcbody(content: Content) -> some View { print(text) return content .onAppear {} } }
It’s hard to see any iOS app which don’t use UITableView or UICollectionView, as they are the basic and important foundation to represent data. UICollectionView is very basic to use, yet a bit tedious for common use cases, but if we abstract over it, then it becomes super hard to customize. Every app is unique, and any attempt to wrap around UICollectionView will fail horribly. A sensable approach for a good abstraction is to make it super easy for normal cases, and easy to customize for advanced scenarios.
I’m always interested in how to make UICollectionView easier and fun to write and have curated many open sources here data source. Many of these data source libraries try to come up with totally different namings and complex paradigm which makes it hard to onboard, and many are hard to customize.
In its simplest form, what we want in a UICollectionView data source is cell = f(state), which means our cell representation is just a function of the state. We just want to set model to the cell, the correct cell, in a type safe manner.
Generic data source
The basic is to make a generic data source that sticks with a particular cell
1 2 3 4 5
classDataSource<T>: NSObject{ let items: [T] let configure: (T, UICollectionViewCell) -> Void let select: (UICollectionViewCell, IndexPath) -> Void }
This works for basic usage, and we can create multiple DataSource for each kind of model. The problem is it’s hard to subclass DataSource as generic in Swift and inheritance for ObjcC NSObject don’t work well.
Check for the types
Seeing the problem with generic data source, I’ve tried another approach with Upstream where it’s easier to declare sections and models.
This uses the Adapter pattern and we need to handle AdapterDelegate. To avoid the generic problem, this Adapter store items as Any, so we need to type cast all the time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
extensionProfileViewController: AdapterDelegate{ funcconfigure(model: Any, view: UIView, indexPath: IndexPath) { guardlet model = model as? Modelelse { return }
switch (model, view) { case (.avatar(let string), let cell asAvatarcell): cell.configure(string: string) case (.name(let name), let cell asNameCell): cell.configure(string: name) case (.header(let string), let view asHeaderView): view.configure(string: string) default: break } } }
The benefit is that we can easily subclass this Adapter manager to customize the behaviour, here is how to make accordion
SwiftUI comes in iOS 13 with a very concise and easy to use syntax. SwiftUI has good diffing so we just need to update our models so the whole content will be diffed and rendered again.
1 2 3 4 5 6 7 8 9 10 11 12
var body: some View { List { ForEach(blogs) { blog in VStack { Text(blog.name) } .onTap { print("cell was tapped") } } } }
SwiftUI style with diffing
I built DeepDiff before and it was used by many people. Now I’m pleased to introduce Micro which is a SwiftU style with DeepDiff powered so it performs fast diffing whenever state changes.
With Micro we can just use the familiar forEach to declare Cell, and the returned State will tell DataSource to update the UICollectionView.
Every time state is assigned, UICollectionView will be fast diffed and reloaded. The only requirement is that your model should conform to DiffAware with diffId so DeepDiff knows how to diff for changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
let dataSource = DataSource(collectionView: collectionView) dataSource.state = State { ForEach(blogs) { blog in Cell<BlogCell>() { context, cell in cell.nameLabel.text = blog.name } .onSelect { context in print("cell at index \(context.indexPath.item) is selected") } .onSize { context in CGSize( width: context.collectionView.frame.size.width, height: 40 ) } } }
DataSource is completely overridable, if you want to customize any methods, just subclass DataSource, override methods and access its state.models
1 2 3 4 5 6
classCustomDataSource: DataSource{ overridefunccollectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let blog = state.models[indexPath.item] as? Blog print(blog) } }
This is iOS 13+ only, and the main components are the cellProvider acting as cellForItemAtIndexPath, and the snapshot for diffing. It also supports section.
1 2
let snapshot = NSDiffableDataSourceSnapshot<Section, Blog>() dataSource.apply(snapshot, animatingDifferences: animate)
Although SearchObject is class, when we use ForEach, the changes to passed object won’t be reflected in our array and there is no reload trigger, we need to point to object in array directly, like
var body: some View { List { ForEach(groups) { group in Section( header: Text(group.initial) .foregroundColor(Color.yellow) .styleTitle(), content: { ForEach(group.countries) { country in CountryRow(country: country) } } ) } } } }
We need to use frame(minWidth: 0, maxWidth: .infinity, alignment: .leading). Note that order is important, and padding should be first, and background after frame to apply color to the entire frame
View extends to the bottom, but not to the notch. We need to add .edgesIgnoringSafeArea(.top) to our TabView to tell TabView to extend all the way to the top.
Note that if we use edgesIgnoringSafeArea(.all) then TabView ‘s bar will be dragged very down and broken.
Mutation is used to mutate state synchronously. Action is like intent, either from app or from user action. Action maps to Mutation in form of Publisher to work with async action, similar to redux-observable
AnyReducer is a type erasure that takes the reduce function