NSURLBookmarkCreationOptions on NSURL

I at first use SuitableForBookmarkFile. However, the database I store the bookmark takes 200MB. Then I change to MinimalBookmark, the database only takes 98KB. That is 1/2040 of 200MB.


In Apple's doc, it says


Specifies that a bookmark created with this option should be created with minimal information. This produces a smaller bookmark that can be resolved in fewer ways.


Anyone could further explain on this? Thanks.

Accepted Reply

You know you don't have to specify either of those options. You can pass 0 or nil or whatever the equivalent is in Swift.


You should only use SuitableForBookmarkFile if you plan to write an alias file using writeBookmarkData(_:toURL:options:error:).


As to exactly what MinimalBookmark includes or leaves out, and exactly which ways of resolving the bookmark are supported or not, Apple doesn't document that. So, you should only use it if space is at an absolute premium and you're willing to accept that the bookmark might not resolve in some situations where it otherwise would if you didn't specify MinimalBookmark.

Replies

You know you don't have to specify either of those options. You can pass 0 or nil or whatever the equivalent is in Swift.


You should only use SuitableForBookmarkFile if you plan to write an alias file using writeBookmarkData(_:toURL:options:error:).


As to exactly what MinimalBookmark includes or leaves out, and exactly which ways of resolving the bookmark are supported or not, Apple doesn't document that. So, you should only use it if space is at an absolute premium and you're willing to accept that the bookmark might not resolve in some situations where it otherwise would if you didn't specify MinimalBookmark.

Thank you for your reply. In your inspiration, I did a test in my code. Here are my results.

I am using let options:[NSURLBookmarkCreationOptions] = [ NSURLBookmarkCreationOptions(rawValue: 0), .MinimalBookmark, .SuitableForBookmarkFile ]


1 with option 0, the bookmark even a little smaller than the .MinimalBookmark option.

2 I can not get a file alias from option other than .SuitableForBookmark

3 the bookmark data on option .SuitableForBookmarkFile is almost as big as a file alias.

4 although you can not get a file alias directly from other option, you can still get then from bookmark to NSURL, then re-get a bookmark with .SuitableForBookmark option, then get the alias.

do {
  try bookmark.writeToURL(newURL, options:.DataWritingAtomic)
  try NSURL.writeBookmarkData(bookmark, toURL: aliasURL, options: NSURLBookmarkFileCreationOptions(0))
}
catch let error as NSError {
  print(error.localizedDescription)
  var isStale:ObjCBool = false
  let newURL = try! NSURL(byResolvingBookmarkData: bookmark, options: .WithoutUI, relativeToURL: nil, bookmarkDataIsStale: &isStale)
  if !isStale.boolValue {
  let newBookmark = try! newURL.bookmarkDataWithOptions(.SuitableForBookmarkFile, includingResourceValuesForKeys: nil, relativeToURL: nil)
  try! NSURL.writeBookmarkData(newBookmark, toURL: aliasURL, options: NSURLBookmarkFileCreationOptions(0))
  }
}


My conclusion: You should always use option 0 or 512(.MinimalBookmark). They are small and fast. When you need to do an alias, you can restore the url from bookmark in your database, then get a full bookmark that can write to an alias file.


Here are the files I am testing.

-rw-r--r--  1 zhaoxin  staff  4433412850  7 23 22:00 1.mkv
-rw-r--r--  1 zhaoxin  staff   807974265  8  1 09:09 2.mkv
-rw-r--r--@ 1 zhaoxin  staff     7330292  8  2 10:31 3.mov


Here is my testing code.

func bookMarkTest(url:NSURL) {
  let basePath = "/Users/zhaoxin/Desktop/test"
  let baseURL = NSURL(fileURLWithPath: basePath)

  let options:[NSURLBookmarkCreationOptions] = [ NSURLBookmarkCreationOptions(rawValue: 0), .MinimalBookmark, .SuitableForBookmarkFile ]

  for anOption in options {
  let bookmark = try! url.bookmarkDataWithOptions(anOption, includingResourceValuesForKeys: nil, relativeToURL: nil)
  let fileName = url.lastPathComponent!
  let newName = fileName.stringByDeletingPathExtension + "." + anOption.rawValue.description + ".bookmark"
  let newURL = NSURL(fileURLWithPath: newName, relativeToURL: baseURL)
  let aliasName = fileName.stringByDeletingPathExtension + "." + anOption.rawValue.description + "." + url.pathExtension!
  let aliasURL = NSURL(fileURLWithPath: aliasName, relativeToURL: baseURL)

  do {
  try bookmark.writeToURL(newURL, options:.DataWritingAtomic)
  try NSURL.writeBookmarkData(bookmark, toURL: aliasURL, options: NSURLBookmarkFileCreationOptions(0))
  }
  catch let error as NSError {
  print(error.localizedDescription)
  }

  }

  }


func testSuit(path:String) {
  let url = NSURL(fileURLWithPath: path)
  bookMarkTest(url)
}


let paths = [
  "/Users/zhaoxin/Desktop/test/1.mkv",
  "/Users/zhaoxin/Desktop/test/2.mkv",
  "/Users/zhaoxin/Desktop/test/3.mov"
]


for aPath in paths {
  testSuit(aPath)
}


Results(without re-get the bookmark)

-rw-r--r--  1 zhaoxin  staff         904  8  2 10:50 1.0.bookmark
-rw-r--r--  1 zhaoxin  staff     3341788  8  2 10:50 1.1024.bookmark
-rw-r--r--@ 1 zhaoxin  staff     3341796  8  2 10:50 1.1024.mkv
-rw-r--r--  1 zhaoxin  staff         928  8  2 10:50 1.512.bookmark
-rw-r--r--  1 zhaoxin  staff  4433412850  7 23 22:00 1.mkv
-rw-r--r--  1 zhaoxin  staff         904  8  2 10:50 2.0.bookmark
-rw-r--r--  1 zhaoxin  staff     3341788  8  2 10:50 2.1024.bookmark
-rw-r--r--@ 1 zhaoxin  staff     3341796  8  2 10:50 2.1024.mkv
-rw-r--r--  1 zhaoxin  staff         928  8  2 10:50 2.512.bookmark
-rw-r--r--  1 zhaoxin  staff   807974265  8  1 09:09 2.mkv
-rw-r--r--  1 zhaoxin  staff         904  8  2 10:50 3.0.bookmark
-rw-r--r--  1 zhaoxin  staff      673188  8  2 10:50 3.1024.bookmark
-rw-r--r--@ 1 zhaoxin  staff      673196  8  2 10:50 3.1024.mov
-rw-r--r--  1 zhaoxin  staff         928  8  2 10:50 3.512.bookmark
-rw-r--r--@ 1 zhaoxin  staff     7330292  8  2 10:31 3.mov