I want to compare a file’s modification timestamp with that of a Realm model object in order to see whether the database knows about the latest version of the file.
To do this, I ask the RealmSwift.Results for its maximumValue. The fileModificationTime property is modelled as an Int64. This is the line that crashes:
let maxTime = results.maximumValue(ofProperty: "fileModificationTime") as Int64?
To get the maximum value.
A crash. Console output: Could not cast value of type '__NSCFNumber' (0x123a4b608) to 'Swift.Int64' (0x113849298).
(lldb) thread backtrace
* thread #1: tid = 0xd37b, 0x0000000117eabf06 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x0000000117eabf06 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x0000000117f184ec libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x0000000117c2b087 libsystem_c.dylib`abort + 129
frame #3: 0x00000001137df995 libswiftCore.dylib`swift::fatalError(unsigned int, char const*, ...) + 149
frame #4: 0x00000001137c0303 libswiftCore.dylib`swift::swift_dynamicCastFailure(void const*, char const*, void const*, char const*, char const*) + 83
frame #5: 0x00000001137c038f libswiftCore.dylib`swift::swift_dynamicCastFailure(swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, char const*) + 127
frame #6: 0x00000001137c2569 libswiftCore.dylib`swift_dynamicCast + 2825
frame #7: 0x00000001137c2eeb libswiftCore.dylib`_dynamicCastFromExistential(swift::OpaqueValue*, swift::OpaqueValue*, swift::TargetExistentialTypeMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, swift::DynamicCastFlags) + 91
frame #8: 0x00000001137c23fd libswiftCore.dylib`swift_dynamicCast + 2461
frame #9: 0x00000001103f4a2a RealmSwift`RealmSwift.Results.minimumValue <A where A1: RealmSwift.MinMaxType> (ofProperty : Swift.String) -> Swift.Optional<A1> with unmangled suffix "_merged" + 154
frame #10: 0x00000001103f4978 RealmSwift`RealmSwift.Results.maximumValue <A where A1: RealmSwift.MinMaxType> (ofProperty : Swift.String) -> Swift.Optional<A1> + 40 at Results.swift:0
* frame #11: 0x000000010f8c3e08 MyApp`FileDataBlocksDB.isFileUpToDate(path="no-collection/IMG_0007.JPG", fileModified=488735056000, self=0x0000608000235460) -> Bool + 392 at FileDataBlocksDB.swift:35
frame #12: 0x000000010f8d190e MyApp`FileEngine.(asset=0x00007fd4006380b0, idx=5, _0=0x00007fff50470faf, self=0x0000608000245d30, progress=0x0000608000133e20, fdbDB=0x0000608000235460) throws -> Promise<Progress>).(closure #2).(closure #1) + 990 at BackupEngine.swift:63
frame #13: 0x000000010f8d1abd MyApp`thunk + 77 at BackupEngine.swift:0
frame #14: 0x0000000110fa998f CoreFoundation`__NSArrayEnumerate + 607
frame #15: 0x000000010fc535c3 Photos`-[PHFetchResult enumerateObjectsUsingBlock:] + 79
frame #16: 0x000000010f8d149b MyApp`BackupEngine.(fetchResult=0x00006080000fcf00, self=0x0000608000245d30, fdbDB=0x0000608000235460) throws -> Promise<Progress>).(closure #2) + 299 at BackupEngine.swift:75
frame #17: 0x000000010f8d1afd MyApp`thunk + 45 at BackupEngine.swift:0
frame #18: 0x000000010f8d4999 MyApp`partial apply for thunk + 89 at BackupEngine.swift:0
frame #19: 0x000000010fe5e3ac PromiseKit`Promise.(resolve=0x000000010fe64170 PromiseKit`partial apply forwarder for reabstraction thunk helper <A> from @callee_owned (@in PromiseKit.Resolution<A>) -> (@out ()) to @callee_owned (@in PromiseKit.Resolution<A>) -> () at Promise.swift, body=0x000000010f8d4940 MyApp`partial apply forwarder for reabstraction thunk helper from @callee_owned (@owned __ObjC.PHFetchResult<__ObjC.PHAsset>) -> (@owned __ObjC.Progress, @error @owned Swift.ErrorProtocol) to @callee_owned (@in __ObjC.PHFetchResult<__ObjC.PHAsset>) -> (@out __ObjC.Progress, @error @owned Swift.ErrorProtocol) at BackupEngine.swift, value=<unavailable>, $error=ErrorProtocol @ 0x00007fff50471360) throws -> A1) -> Promise<A1>).(closure #1).(closure #1) + 172 at Promise.swift:251 [opt]
frame #20: 0x000000010fe6523a PromiseKit`partial apply for Promise.(then<A> (on : DispatchQueue, execute : (A) throws -> A1) -> Promise<A1>).(closure #1).(closure #1) + 138 at Promise.swift:0 [opt]
frame #21: 0x000000010fe608da PromiseKit`(block=0x000000010fe651b0 PromiseKit`partial apply forwarder for PromiseKit.Promise.(then <A> (on : __ObjC.DispatchQueue, execute : (A) throws -> A1) -> PromiseKit.Promise<A1>).(closure #1).(closure #1) at Promise.swift, resolve=0x000000010fe64170 PromiseKit`partial apply forwarder for reabstraction thunk helper <A> from @callee_owned (@in PromiseKit.Resolution<A>) -> (@out ()) to @callee_owned (@in PromiseKit.Resolution<A>) -> () at Promise.swift) -> (), block : () throws -> ()) -> ()).(closure #1) + 58 at Promise.swift:536 [opt]
frame #22: 0x0000000113e554a4 libswiftDispatch.dylib`partial apply forwarder for reabstraction thunk helper from @callee_unowned @convention(block) () -> () to @callee_owned () -> () + 36
frame #23: 0x0000000117b15637 libdispatch.dylib`_dispatch_call_block_and_release + 12
frame #24: 0x0000000117b36fdf libdispatch.dylib`_dispatch_client_callout + 8
frame #25: 0x0000000117b1e4f6 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1054
frame #26: 0x0000000110fc6b39 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
frame #27: 0x0000000110f8d1ed CoreFoundation`__CFRunLoopRun + 2205
frame #28: 0x0000000110f8c6ed CoreFoundation`CFRunLoopRunSpecific + 285
frame #29: 0x0000000110557920 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 274
frame #30: 0x0000000120cd1430 XCTest`-[XCTestDriver _checkForTestManager] + 258
frame #31: 0x0000000120d20105 XCTest`_XCTestMain + 628
frame #32: 0x0000000110fa887c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
frame #33: 0x0000000110f8d564 CoreFoundation`__CFRunLoopDoBlocks + 356
frame #34: 0x0000000110f8ccd5 CoreFoundation`__CFRunLoopRun + 901
frame #35: 0x0000000110f8c6ed CoreFoundation`CFRunLoopRunSpecific + 285
frame #36: 0x0000000116a46a75 GraphicsServices`GSEventRunModal + 161
frame #37: 0x0000000111e003b9 UIKit`UIApplicationMain + 159
frame #38: 0x000000010f8d67bf MyApp`main + 111 at AppDelegate.swift:12
frame #39: 0x0000000117b8368d libdyld.dylib`start + 1
let realm = try! Realm()
let results = realm.allObjects(ofType: FileDataBlockDBEntry.self)
let maxFileModificationTime = results.maximumValue(ofProperty: "fileModificationTime") as Int64?
This commenter seems to have had the same problem:
http://stackoverflow.com/questions/30218827/unable-to-invoke-max-with-an-argument-list-of-type-string#comment62338400_30220258
ProductName: Mac OS X
ProductVersion: 10.11.5
BuildVersion: 15F34
/Applications/Xcode-beta.app/Contents/Developer
Xcode 8.0
Build version 8S162m (beta 2)
/Users/Yang/.rvm/rubies/ruby-2.2.1/bin/pod
0.37.2
(not in use here)
/bin/bash
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)
/usr/local/bin/carthage
0.17.1
github "realm/realm-cocoa" "3fda82279ee4c93ee57b63e5524f83e3a496215f"
/usr/bin/git
git version 2.8.4 (Apple Git-73)
Thanks :)
My current workaround:
let maxTime = results.sorted(onProperty: "fileModificationTime", ascending: false).first?.fileModificationTime
Thanks for sharing this @yangmeyer. I could reproduce this easily with your instructions. I've labeled this as a bug and hopefully we can resolve quickly.
According to the Swift documentation,
All of the following types are automatically bridged to NSNumber: Int, Uint, Float, Double, Bool
We're incorrectly assuming that types like Int64 and friends are also bridged, and they are not.
Most helpful comment
According to the Swift documentation,
We're incorrectly assuming that types like
Int64and friends are also bridged, and they are not.