Enable NSArrayController predicate based on checkbox - cocoa

I've got a static filter to turn on/off for an NSArrayController based on whether or not a checkbox is checked. Right now I've bound the checkbox value to this:
private dynamic var filterPending: NSNumber! {
willSet {
willChangeValueForKey("filterPredicate")
}
didSet {
didChangeValueForKey("filterPredicate")
}
}
and then I bound the filter of the NSArrayController to this:
private dynamic var filterPredicate: NSPredicate? {
guard let filter = filterPending?.boolValue where filter == true else { return nil }
return NSPredicate(format: "pending > 0")
}
That seems to work properly, but feels like maybe I'm missing some easier way of doing this?

In your set-up the value of filterPredicate depends on the value of filterPending. As Gerd K points out, the Key-Value Observing API allows you to specify this type of relationship by registering filterPending as a dependent key of filterPredicate:
// MyFile.swift
class func keyPathsForValuesAffectingFilterPredicate() -> Set<NSObject> {
return Set<NSObject>(arrayLiteral: "filterPending")
}
private dynamic var filterPending: NSNumber!
private dynamic var filterPredicate: NSPredicate? {
guard let filter = filterPending?.boolValue where filter == true else { return nil }
return NSPredicate(format: "pending > 0")
}

Related

How Can I Tell If My NSManagedObject Resides in a Read Only NSPersistentStore?

I would like to add read-only example/tutorial data to my Core Data based macOS app.
I will include an SQL file in my application bundle containing the example data. My NSPersistentContainer will have 2 NSPersistentStores, one writable and one read only. I will only have a default configuration for my model since both stores will have the same model.
My UI will need to know if the data displayed is read only or not, for example, to stop this data being draggable.
I know that NSManagedObject does not support a readonly state, see and : Is it possible to return NSManagedObjects as read-only in Core Data? ...and the docs.
I think the best approach would be to add a readonly property to my NSManagedObject derived class that can be queried where necessary. However, I can't see how I could easily set this property! I can't find a direct link to an NSPersistentStore from an NSManagedObject.
I could set up an NSFetchRequest and specify the read only store and see if the NSManagedObject is in it, but that seems a little ridiculous.
Am I missing something more obvious here please?
With thanks to pbasdf for his suggestion...
I could find no straight-forward way to achieve this. I had to move away from using NSPersistentContainer to simplify my Core Data stack. However, I think this is a fairly elegant solution if you need a small subset of your graph to be readonly.
I subclassed NSPersistentStoreCoordinator to cache the NSManagedObjectIDs of any readonly store added to it:
class GraphStoreCoordinator: NSPersistentStoreCoordinator
{
override init(managedObjectModel model: NSManagedObjectModel)
{
readOnlyTestContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
super.init(managedObjectModel: model)
readOnlyTestContext.persistentStoreCoordinator = self
NotificationCenter.default
.addObserver(forName: .NSPersistentStoreCoordinatorStoresDidChange,
object: self, queue: nil) { [unowned self] notification in
// userInfo will be in this form for add/remove keys - not supporting migration here
guard let userInfo = notification.userInfo as? [String: [NSPersistentStore]] else {
unhandledError("Invalid userInfo for NSPersistentStoreCoordinatorStoresDidChange.") }
userInfo[NSAddedPersistentStoresKey]?.forEach { self.didAddStore($0) }
userInfo[NSRemovedPersistentStoresKey]?.forEach { self.didRemoveStore($0) }
}
}
deinit {
NotificationCenter.default
.removeObserver(self, name: .NSPersistentStoreCoordinatorStoresDidChange, object: self)
}
private func didAddStore(_ store: NSPersistentStore) {
guard store.isReadOnly else { return }
var addedObjects = Set<NSManagedObjectID>()
baseEntityNames.forEach { entityName in
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName)
fetchRequest.affectedStores = [store]
do {
let addedEntityObjects = try readOnlyTestContext.fetch(fetchRequest)
addedObjects = addedObjects.union(addedEntityObjects.map { $0.objectID })
} catch {
unhandledError("Failed to fetch all \(entityName) for read only check: \(error)") }
}
readOnlyObjects[store.identifier] = addedObjects
}
private func didRemoveStore(_ store: NSPersistentStore) {
guard store.isReadOnly else { return }
readOnlyObjects.removeValue(forKey: store.identifier)
}
/// Returns the minimum set of entities that can be fetched for readonly checking
private lazy var baseEntityNames: [String] = {
return managedObjectModel.entitiesByName.compactMap { $1.superentity == nil ? $0 : nil }
}()
private var readOnlyTestContext: NSManagedObjectContext
/// Readonly objectIDs keyed per persistent store
private var readOnlyObjects = [String : Set<NSManagedObjectID>]()
internal func isObjectReadOnly(_ objectID: NSManagedObjectID) -> Bool {
return readOnlyObjects.contains(where: { $1.contains(objectID) } )
}
}
I then added an extension to NSManagedObject to that queries its NSPersistentStoreCoordinator for read-only status:
public extension NSManagedObject
{
/// Does this managed object reside in a read-only persistent store?
var isReadOnly: Bool {
guard let coordinator = managedObjectContext?
.persistentStoreCoordinator as? GraphStoreCoordinator else {
unhandledError("Should only check readonly status in a GraphStoreCoordinator") }
return coordinator.isObjectReadOnly(objectID)
}
}

Chain propagation of a RxSwift Variable to another Variable

This is an example of our use case:
We have a selectedIndex and a list of items.
class FoosViewModel {
let selectedIndex = Variable<Int>(0)
let items: [Foo] = ... // assume that this is initialized properly
}
In reality, we often care about which item is selected instead of the index of the selected item. So we'll have code like this:
selectedIndex.asObservable().subscribe(onNext: { [weak self] index in
guard let self = self else { return }
let selectedItem = items[index]
// Do sth with `selectedItem` here
}
Notice that the value of selectedItem is always driven by selectedIndex. Therefore, we change the code to the following:
class FoosViewModel {
let selectedIndex = Variable<Int>(0)
let selectedItem = Variable<Int>(items[0])
let items: [Foo] = ... // assume that this is initialized properly
init() {
selectedIndex.asObservable().subscribe(onNext: { [weak self] index in
guard let self = self else { return }
self.selectedItem = items[index]
}
}
}
This seems to be a common enough use case. Do we have an existing operator in Rx that can map a Variable to another? Is there sth like this:
class FoosViewModel {
let selectedIndex = Variable<Int>(0)
let selectedItem = selectedIndex.map{ items[$0] }
let items: [Foo] = ... // assume that this is initialized properly
}
What you have done is created two bits of state that are dependent on each other. It would be better to just have one source of truth and a derivative which means that one should be implemented differently than the other. Assuming that selectedIndex is the source of truth, then I would expect to see:
class FoosViewModel {
let selectedIndex = Variable<Int>(0)
let selectedItem: Observable<Foo?>
let items: [Foo]
init(items: [Foo]) {
selectedItem = selectedIndex.asObservable().map { index in
index < items.count ? items[$0] : nil
}
self.items = items
}
}
Unlike in your attempt, there is no temptation for a user of this class to try to assign a new value to selectedItem (in fact, the code won't even compile if you try.) As a side benefit, there is no need to do the "weak self dance" either since the map doesn't refer to self at all. All of this works because you made items a let rather than a var (good for you!)
If you wanted to be able to add/remove items then things get a bit more complex...
class MutableFooViewModel {
let selectedIndex = Variable<Int>(0)
let selectedItem: Observable<Foo?>
let items = Variable<[Foo]>([])
init(items: [Foo]) {
items.value = items
let _items = self.items // this is done to avoid reference to `self` in the below.
selectedItem = Observable.combineLatest(
_items.asObservable(),
selectedIndex.asObservable()
) { items, index in
index < items.count ? items[index] : nil
}
}
}
The idea here is that Subjects (Variable is a kind of subject) should not be the first thing you think of when making an Observable that depends on some other observable. In this respect, they are only good for creating the initial observable. (RxCocoa is full of them.)
Oh and by the way, Variable had been deprecated.

Best Practice or method to see if the form changed from its initial values or not

I apologize if this is covered elsewhere, but I haven't been able to find a reference to this.
Is there a built-in method or Best Practice to identifying which rows had changed from their initial values?
So here is what I ended up doing - and this has been working well. This is a partial class implementation just for this purpose:
class YourFormViewController : FormViewController {
var formValuesChanged = [String : (Any?, Any?)]()
override func valueHasBeenChanged(for row: BaseRow, oldValue: Any?, newValue: Any?) {
super.valueHasBeenChanged(for: row, oldValue: oldValue, newValue: newValue)
guard let rowIdentifier = row.tag else { return }
formValuesChanged[rowIdentifier] = (oldValue, newValue)
}
}
I use this as a base class for all my forms and then I can just use:
guard formValuesChanged.count > 0 else {
log.info("Did not save editor because no values were changed")
dismiss(animated: true, completion: nil)
return
}

computed setter for a subscript of an array in Swift [duplicate]

This question already has answers here:
Shorthand setter declaration for a subscript of an array in Swift
(2 answers)
Closed 8 years ago.
To keep it short, what I want to achieve is for example:
var actions: [String]{
get{
if (_actions==nil){
_actions = []
}
return _actions!
}
set{
_actions = newValue
}
subscript(index:Int) -> String{
set {
assert(index<_actions.count && index>=0, "Index out of range")
_actions[index] = newValue
}
}
}
I know subscript isn't an accessor for array, but then what is the most convinient alternative to do just that?
I truly appreciate for succinct answers if possible! Thank you very much!
Edit:
To extend my explanation for #jrturton,
What I am trying to achieve is whenever actions[i] is set to a newValue, I would like to do some extra computations, such as repositioning actions[i]'s respective subview.
But if i say actions[3] = "randomMethod", the computed setter for the entire array will get called. Right? So I'd like to find a way so that when actions[3] is set to a newValue, a function repositionView(3) can get called, for example.
I know other ways to do it, but my question simply askes if there is a more convinient way, like the example above: a computed setter, to do what I want?
Edit 2:
To show #Vatsal Manot what I truly mean, I removed getter for subscript, and here is a complete example.swift(which wont run due to error):
import UIKit
import Foundation
class DWActionsSubmenu: UIView{
var actions: [DWAction]{
get{
if (_actions==nil){
_actions = []
}
return _actions!
}
set{
_actions = newValue
}
subscript(index:Int) -> DWAction{
set {
assert(index<_actions.count && index>=0, "Index out of range")
_actions[index] = newValue
a()
}
}
}
var _actions: [DWAction]?
init(actions:[DWAction]?){
super.init()
_actions = actions
}
required init(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
}
func a(){
}
}
I'd wrap your actions list in a custom class that you can then access via subscripting. You can then add a block to be run whenever a subscripted member is set:
class ActionList {
private var actions = [String]()
var actionDidChange : ((Int) -> ())?
subscript(actionIndex:Int) -> String {
get {
return actions[actionIndex]
}
set {
actions[actionIndex] = newValue
if let actionDidChange = actionDidChange {
actionDidChange(actionIndex)
}
}
}
func addAction(action: String) {
actions.append(action)
}
func addActions(newActions:[String]) {
actions += newActions
}
}
Usage (in a playground):
let actionList = ActionList()
actionList.actionDidChange = {
actionIndex in
println("Action \(actionIndex) did change")
}
actionList.addActions(["One", "Two", "Three"])
actionList[2] = "New"
// Prints "Action 2 did change"
The following should work:
var actions: [String] = []
subscript(index:Int) -> String
{
get
{
assert(index < actions.count && index >= 0, "Index out of range")
return actions[index]
}
set(newValue)
{
assert(index < actions.count && index >= 0, "Index out of range")
actions[index] = newValue
}
}

are needsDisplayForKey/actionForKey overrides working correctly?

I'm trying to convert some code that works in Objective-C to Swift. The problem I'm running into is that needsDisplayForKey/actionForKey aren't getting called the same way. As far as I can tell, the custom key values aren't getting passed in correctly. Here is what I get when I debug it:
default value:
(String!) event = {
core = {
_baseAddress = Builtin.RawPointer = 0x00feee51 "onOrderIn"
_countAndFlags = 1073741833
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x01026348 #"onOrderIn")
}
}
}
custom value (empty string passed in):
(String!) event = {
core = {
_baseAddress = Builtin.RawPointer = 0x0b418f79
_countAndFlags = 1073741833
_owner = Some {
Some = (instance_type = Builtin.RawPointer = 0x0b418f70 -> 0x006e38f0 (void *)0x006e38c8: __NSCFString)
}
}
}
I'm not sure what the relevant code might be. I'll just ask - has anyone else was able to define a custom implicit animation in Swift? Is there anything I need to keep in mind when moving over from Objective C?
override class func needsDisplayForKey(key: String!) -> Bool{
if key == "angleFrom" || key == "angleTo" {
return true;
}
return super.needsDisplayForKey(key)
}
override func actionForKey(event: String!) -> CAAction!{
if event == "angleFrom" || event == "angleTo" {
return self.makeAnimationForKey(event)
}
return super.actionForKey(event)
}
I got this working in Swift by using #NSManaged attribute in front of the variable declaration (where you would use the the #dynamic attribute in Objective-C)

Resources