Gieta Laksmana
  • Blog
  • Profile
  • Projects
    • VR World Arcade
    • Punch For Fun
    • Spectrum
    • Dimension Battle
  • Resume

CoreData on macOS Mojave (10.14.0) shenanigans

9/26/2018

0 Comments

 
UPDATE (10/16/2018): Apple has closed my bug report about this as resolved. So hopefully the fix will be out on Mojave 10.14.1

tldr: overwriting a binary blob greater than 100kb will unnecessarily create extra files on each save with CoreData on macOS Mojave (10.14.0)
​Hookay, I was pretty excited to try out macOS Mojave and their dark mode. Instead I found a bug in CoreData

If you'd like to follow along the code, the code is available at my github:
https://github.com/gietal/AppleSandbox/tree/master/CoreData/CoreDataPlayground

Quick overview of CoreData, it stores your data in a sqlite database which live in your App's "Application Support" folder under ~/Library/<Your app bundle identifier> or ~/Library/Containers/<Your app bundle identifier>/Data/Library for sandboxed apps. You can store different types of data with CoreData such as string, int, float, bool, and even binary blob. When storing binary blob, you have an option to store it on an external storage:
Picture
​This is useful for bigger binary blob, as it’s less efficient to store and fetch a big binary blob form the sqlite database. If you try to store a big binary blob (bigger than 100kb), and you have that option enabled, CoreData will automatically decide to store your blob into a hidden folder called <Your DB name>_Support/_EXTERNAL_DATA
Picture
​The blob will be stored as a file named with a random UUID, and that UUID is what’s then stored in the sqlite database. You can examine your sqlite database content with DB Browser for SQLite (free from https://sqlitebrowser.org). You can see that the value for my binary blob attribute is the UUID of the file under _EXTERNAL_DATA. This is how CoreData manages your big binary blob.
Picture
Picture
The value of the binary attribute in the sqlite DB is actually the UUID name of the file on the _EXTERNAL_DATA folder
​When you update your binary blob from your code, then the blob file under _EXTERNAL_DATA is also overwritten with the new blob. However, this is not the case in macOS Mojave 10.14.0, which is demonstrated by my sample project

The sample project is quite simple, I have have an Entity with 2 simple attributes: an id (string) and an image (binary blob) with “Allows External Storage” selected. The id is so that I can fetch the same instance of the entity. When you run the app, you will see a window with a save button. 
Picture
Picture
Each time you hit the save button, it will try to save the displayed image as a binary blob to the entity’s instance in the sqlite database. Since the binary blob is greater than 100kb, it will store it under:
~/Library/Containers/com.gietal.sandbox.CoreDataPlayground/Data/Library/Application Support/CoreDataPlayground/.CoreDataPlayground_SUPPORT/_EXTERNAL_DATA

If you hit the save button multiple times, it will try to overwrite the binary blob with the same image every time. Now here’s where the problem lies. On macOS High Sierra (10.13.6) there is no issue with this, the binary blob is overwritten correctly. On macOS Mojave (10.14.0) however, this is not the case.

When you try running the same app on macOS Mojave, and start hitting the save button multiple times, you will observe that a new file keeps getting added under the _EXTERNAL_DATA folder. This is a serious problem as overtime the amount of useless files keep getting piled up inside this hidden folder. Normal non-savvy users would never know how their mac ran out of space so quickly due to this issue.

I submitted this issue to Apple’s Bug Reporter, and they acknowledged that it was a bug and are working on a fix. Hopefully macOS Mojave 10.14.1 come out soon with a fix for this.

A workaround you can do at the moment is to save the entity’s instance with a nil value for the binary attribute, and save it so that CoreData delete the external file. Then, you can save the instance again with the proper binary blob.
// magic functions to get the CoreData context and entity's instance
let context = getContext()
let entity = getEntity()

// nil out the binary blob, so CoreData erases the file in _EXTERNAL_DATA
entity.setValue(nil, forKey: "image")
context.save()

// then actually save your new blob
entity.setValue(blob, forKey: "image")
context.save()
0 Comments

Main and Key Windows Shenanigans

8/18/2018

0 Comments

 
Not sure if this is the case in general, or just some corner case that I hit while being unlucky.

What I found is that when an app becomes active, there has to be 1 main window on the same space as the current key window. However, this only happens to me when Displays Have Separate spaces is enabled while having multi monitor setup:
Picture
​My specific scenario:
  • Have 2 monitors A and B
  • Create an app with 2 windows:
    • Window 1: can become main window and key window
    • Window 2: can become key window, cannot become main window
  • Move Window 1 to Monitor A
  • Move Window 2 to Monitor B
  • Deactivate the app by going to another app (Finder or something)
  • Click Window 2 on Monitor B to activate the app

Expectation:
  • Window 2 is activated and is the key window (have highlight on the window title bar)
Actual observed behavior:
  • Window 2 is activated
  • Then immediately after that, Window 1 is activated

This produce a jarring effect where clicking on a window actually activates another window and shift the active window to that window instead.

One solution is to make Window 2 to be able to become main window, but there might be a case where you might not want that.
0 Comments

NSWindow positioning shenanigans

8/4/2018

0 Comments

 
You're playing around with NSWindow, and you find this function called setFrame, setFrameOrigin, and setFrameTopLeftPoint. You feel like you are the king of the world when the thought of being able to move your window anywhere programmatically consume you. Except that when you realized that Cocoa stops you from doing that.

Cocoa allows you to move the window anywhere using those functions, except when your window starts going above the menu bar.

For instance, consider the following scenario:
  • Screen Configuration:
    • Resolution at 1280 x 800
    • Menu bar height = 22 pixel
    • Which means, the menu bar starts at y = 778 (Cocoa coordinate start at the bottom left)
  • Your Window:
    • Size is 100 x 100

When you call window.setFrameTopLeftPoint with the position of (x: 100, y: 800) your window's top left will actually be positioned at (x: 100, y: 778), right under the menu bar.

Same goes with the other setFrame* variants, your window will be pushed down until no part of your window is above the menu bar.

This is of course unless your window is the size (or bigger) than the screen, in which case it has no choice.
0 Comments

NSSecureTextField Shenanigans

7/14/2018

1 Comment

 
Interesting finding:

From the get go, you're not able to input accented characters with keyboard shortcut (alt+e e, alt+u o, etc) on a NSSecureTextField.
But I find this interesting "feature" (maybe a bug or loophole in Apple's implementation of secure text field?) which allows you to do just that.

Here's what to do:

Make sure key repeat is on at System Preference > Keyboard > Keyboard
Picture
I created a simple app with a basic NSSecureTextField and a label to display the content of the secure text field.

​Go to the NSSecureTextField
​Hold e (or any letter that has accented character), until the key repeats
Picture
After this point, key combinations (alt+e e, alt+y, alt+u o, etc) will work as expected ​
Picture
1 Comment

NSPopUpButton shenanigans

7/8/2018

0 Comments

 
​action/value bound to NSPopupButton is only triggered when user changes the value from the UI. It doesnt get triggered when you change the value programmatically.

For example, if we bind the NSPopupButton's selected index property:
​
Picture
internal var selectedIndex: AnyObject! {
    didSet {
        let oldIndex = oldValue as? Int
        let newIndex = selectedIndex as? Int
        print("selectedIndex didSet, old: \(oldIndex), new: \(newIndex)")
    }
}
changing the selected value via the UI will trigger selectedIndex.didSet as expected
Picture
However, when we change the index programmatically such as this:
popUpButton.selectItem(at: 2)
Doing so will change the value in the UI, but it won't trigger selectedIndex.didSet as expected.

A bigger issue happens when we have this scenario:
  • ​User selects index 1 from the UI
  • We programmatically call popupButton.selectItem(at: 2), the UI changed to show that index 2 is selected
  • User reselects index 1 from the UI
  • selectedIndex.didset is not called for the user action!!
       
This happens because when we change the selected item programmatically to 2, selectedIndex's value doesn't actually change, it stayed at 1. So when the user change the index back to 1 via the UI, Cocoa realized that the value of selectedIndex is still the same (1), so it decided not to call the didset function.
0 Comments

UserDefaults shenanigans

9/7/2017

0 Comments

 
It seems like the UserDefaults or at least the shared group UserDefaults is being cached somewhere, and it can put you in a confused debugging state.

To highlight what I meant, create 2 sandboxed apps with the same app group, i.e: com.gietal.sandbox.group

On the first app, do the following:
// App 1
if let defaults = UserDefaults(suiteName: "com.gietal.sandbox.group") {
    defaults.set(123, forKey: "myKey")
}
When you run the first app, it should create this file ~/Library/Group Containers/com.gietal.sandbox.group/Library/Preferences/com.gietal.sandbox.group.plist where the setting "myKey = 123" is stored as xml.

​Now on the second app, try to read the value from the shared group UserDefaults. At this point, it should return the expected value of 123
// App 2
if let defaults = UserDefaults(suiteName: "com.gietal.sandbox.group") {
    let value = defaults.value(forKey: "myKey")
    print("value: \(value)")
}
Now delete the plist file ~/Library/Group Containers/com.gietal.sandbox.group/Library/Preferences/com.gietal.sandbox.group.plist

And try reading the value again from app 2. You would be surprised that it would still returns 123 instead of the expected nil.

However, if you reboot your mac and try reading the value of "myKey" from app 2 again, it will correctly returns nil this time!

So this made me believe that UserDefaults is being cached somewhere in the system and could get out of sync with the actual plist file where the values are stored.
0 Comments

Xcode 8.1 and Swift 3.0.1 shenanigans

11/29/2016

0 Comments

 
When upgrading my xcode from 8.0 to 8.1 and Swift to 3.0.1, I found that I can no longer compare type with implicitly unwrapped optional in a switch statement. I need to explicitly unwrap the optional
// this no longer works
let a: String! = "asd"
let b: String = "asd"
switch a {
case b: // error: expression pattern of type 'String' cannot match values of type 'String!'
    break
default:
    break
}
0 Comments

MacOS Multi Monitor Shenanigans

10/25/2016

0 Comments

 
let's say you want your Mac app to have multiple windows, pretty reasonable request right? Now, lets say you have 2 monitor setup on your Mac, and you like to have each window on its own screen. Move one window to the first screen, and move the other window to the second screen, got it. Now what if you want both to be full screened on each screen?

if you have this setting under System Preferences > Mission Control enabled,
then you wont hit this problem. But if you're like me and have it disabled, you might be wondering why your secondary monitor stays black. keep on reading...
Picture
Displays? Spaces?

One of the key feature in MacOS is Spaces. It's that thing that you see at the top when you go to Mission Control (usually by pressing F3 in most Mac keyboard) that helps you organize and declutter apps windows.

The deal with the setting I mentioned in the beginning has to do with what happen when you attach a second, or third, or 'n'th monitor to your Mac. If you have the setting enabled, it will create a Space for each monitor. But if you disable it, all monitors will share the same space.

This isn't really an issue until you want your app to be fullscreened. When a window is changed to full screen, MacOS will create a new exclusive space for it and move the window there.

So when you make 1 of your window a fullscreen window, a new exclusive space will be created, and only your window will be placed there. To add your other windows to this fullscreen space, you need to do this to your window:
window.collectionBehavior.remove(.fullScreenPrimary)
window.collectionBehavior.insert(.fullScreenAuxiliary)
0 Comments

    Archives

    September 2018
    August 2018
    July 2018
    September 2017
    July 2017
    November 2016
    October 2016
    September 2016
    June 2016
    May 2016
    March 2016

    Categories

    All
    Shenanigans
    Software Engineering
    Swift
    Vr
    Xcode

    RSS Feed

Powered by Create your own unique website with customizable templates.