Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using Realm in Swift Playgrounds #938

Closed
leonbreedt opened this issue Sep 29, 2014 · 15 comments
Closed

Support using Realm in Swift Playgrounds #938

leonbreedt opened this issue Sep 29, 2014 · 15 comments

Comments

@leonbreedt
Copy link

Hi,

I realise this is probably full of sharp edges, but I thought I'd try to use Realm in a playground (git HEAD, commit 2990f07). I'm on Xcode 6.0.1 GM (6A317).

I followed the instructions for getting started with Swift, and Realm is visible to my Swift application.
I can also import it into my playground, and the code completion works 😎

This is the code in the playground:

import Realm
class Dog : RLMObject {
}
let d = Dog()

But the final let statement crashes, clicking the inspect button shows this call stack 😱

* thread #1: tid = 0x4216c, 0x0000000109003e57 libswiftCore.dylib`swift_ClassMirror_count + 23, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
  * frame #0: 0x0000000109003e57 libswiftCore.dylib`swift_ClassMirror_count + 23
    frame #1: 0x0000000107174269 PlaygroundLogger`PlaygroundLogger.PlaygroundObjectWriter.encodeStructured (PlaygroundLogger.PlaygroundObjectWriter)(Swift.MirrorType, Swift.UInt64, PlaygroundLogger.LoggerCappingPolicy) -> () + 121
    frame #2: 0x0000000107174ee0 PlaygroundLogger`PlaygroundLogger.PlaygroundObjectWriter.encodeStructured (PlaygroundLogger.PlaygroundObjectWriter)(Swift.MirrorType, Swift.UInt64) -> () + 656
    frame #3: 0x000000010717ad80 PlaygroundLogger`PlaygroundLogger.PlaygroundObjectWriter.encode (PlaygroundLogger.PlaygroundObjectWriter)(Swift.MirrorType, Swift.UInt64) -> () + 7728
    frame #4: 0x000000010717b4c0 PlaygroundLogger`PlaygroundLogger.PlaygroundObjectWriter.encodeObject (PlaygroundLogger.PlaygroundObjectWriter)<A>(A, Swift.String) -> () + 1296
    frame #5: 0x000000010714efde PlaygroundLogger`playground_log + 350
    frame #6: 0x0000000113013638 $__lldb_expr414`top_level_code + 168 at playground414.swift:8
    frame #7: 0x00000001130139dd $__lldb_expr414`main + 61 at <EXPR>:0
    frame #8: 0x00000001070aa510 HouseFinder`get_field_types__XCPAppDelegate + 160
    frame #9: 0x00000001070ab081 HouseFinder`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 17
    frame #10: 0x00000001070a9cf1 HouseFinder`partial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 81
    frame #11: 0x00000001070ab0b0 HouseFinder`reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to @callee_owned () -> (@unowned ()) + 32
    frame #12: 0x00000001070ab0e7 HouseFinder`reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39
    frame #13: 0x00000001076f2abc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #14: 0x00000001076e8805 CoreFoundation`__CFRunLoopDoBlocks + 341
    frame #15: 0x00000001076e7fc3 CoreFoundation`__CFRunLoopRun + 851
    frame #16: 0x00000001076e7a06 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #17: 0x00000001077942c1 CoreFoundation`CFRunLoopRun + 97
    frame #18: 0x00000001070a7dcd HouseFinder`top_level_code + 3629
    frame #19: 0x00000001070aa53a HouseFinder`main + 42
    frame #20: 0x000000010a3c5145 libdyld.dylib`start + 1

It is unclear whether it's a bug in Swift, the playground infrastructure, or something Realm does. Regardless, it would be pretty sweet to have playground support for rapid prototyping, so I just thought I'd put this up here.

Leon

@bmunkholm
Copy link
Contributor

Did you also try to add a property in the class?

On Mon, Sep 29, 2014 at 10:19 AM, Leon Breedt notifications@github.com
wrote:

Hi,

I realise this is probably full of sharp edges, but I thought I'd try to
use Realm in a playground.

I followed the instructions for getting started with Swift, and Realm is
visible to my Swift application.
I can also import it into my playground, and the code completion works [image:
😎]

This is the code in the playground:

import Realmclass Dog : RLMObject {}let d = Dog()

But the final let statement crashes, clicking the inspect button shows
this call stack [image: 😱]

  • thread Column type rename #1: tid = 0x4216c, 0x0000000109003e57 libswiftCore.dylib`swift_ClassMirror_count + 23, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
    • frame #0: 0x0000000109003e57 libswiftCore.dylibswift_ClassMirror_count + 23 frame #1: 0x0000000107174269 PlaygroundLoggerPlaygroundLogger.PlaygroundObjectWriter.encodeStructured (PlaygroundLogger.PlaygroundObjectWriter)(Swift.MirrorType, Swift.UInt64, PlaygroundLogger.LoggerCappingPolicy) -> () + 121
      frame Support for 'float' and 'double' column types and a lot of other fixes #2: 0x0000000107174ee0 PlaygroundLoggerPlaygroundLogger.PlaygroundObjectWriter.encodeStructured (PlaygroundLogger.PlaygroundObjectWriter)(Swift.MirrorType, Swift.UInt64) -> () + 656 frame #3: 0x000000010717ad80 PlaygroundLoggerPlaygroundLogger.PlaygroundObjectWriter.encode (PlaygroundLogger.PlaygroundObjectWriter)(Swift.MirrorType, Swift.UInt64) -> () + 7728
      frame Explicit string size - Obj-C #4: 0x000000010717b4c0 PlaygroundLoggerPlaygroundLogger.PlaygroundObjectWriter.encodeObject (PlaygroundLogger.PlaygroundObjectWriter)<A>(A, Swift.String) -> () + 1296 frame #5: 0x000000010714efde PlaygroundLoggerplayground_log + 350
      frame Tutorial (./examples/tutorial.m) ready for inspection and upload if found ok. #6: 0x0000000113013638 $lldb_expr414top_level_code + 168 at playground414.swift:8 frame #7: 0x00000001130139dd $__lldb_expr414main + 61 at :0
      frame Fix for memory leakage when typed tables are created in a group. #8: 0x00000001070aa510 HouseFinderget_field_types__XCPAppDelegate + 160 frame #9: 0x00000001070ab081 HouseFinderreabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 17
      frame Adds new unit test for cursors. #10: 0x00000001070a9cf1 HouseFinderpartial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) + 81 frame #11: 0x00000001070ab0b0 HouseFinderreabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to @callee_owned () -> (@unowned ()) + 32
      frame Update examples #12: 0x00000001070ab0e7 HouseFinderreabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) + 39 frame #13: 0x00000001076f2abc CoreFoundation__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK
      + 12
      frame added xcode project to examples #14: 0x00000001076e8805 CoreFoundation__CFRunLoopDoBlocks + 341 frame #15: 0x00000001076e7fc3 CoreFoundation__CFRunLoopRun + 851
      frame Now using new Descriptor based API (Spec is no longer a public class) #16: 0x00000001076e7a06 CoreFoundationCFRunLoopRunSpecific + 470 frame #17: 0x00000001077942c1 CoreFoundationCFRunLoopRun + 97
      frame using correct compiler + added 2x default image #18: 0x00000001070a7dcd HouseFindertop_level_code + 3629 frame #19: 0x00000001070aa53a HouseFindermain + 42
      frame Typo in documentation. Can now generate again. #20: 0x000000010a3c5145 libdyld.dylib`start + 1

It is unclear whether it's a bug in Swift, the playground infrastructure,
or something Realm does. Regardless, it would be pretty sweet to have
playground support for rapid prototyping, so I just thought I'd put this up
here.

Leon


Reply to this email directly or view it on GitHub
#938.

@leonbreedt
Copy link
Author

Yes, I started out with the sample from realm.io:

class Dog: RLMObject {
    dynamic var name = ""
    dynamic var age = 0
}

@jpsim
Copy link
Contributor

jpsim commented Sep 29, 2014

You're right this is full of sharp edges 😎. From the stack trace, it looks like there are issues with Swift's mirroring API, which is odd because we stopped using that directly a while back, though I'm sure the Swift standard library uses it somewhere.

I'd say please make sure you're running in an iOS Playground and you're importing the iOS version of Realm, as your version of Xcode doesn't support OSX Playgrounds.

Though my guess is that it's something in the way Realm works that's preventing it from running. Hopefully we get a chance to troubleshoot this in the near future. Until then, please update us with any progress.

@alazier alazier added the backlog label Dec 5, 2014
@donald-pinckney
Copy link

For me Realm works in a playground, building 691ef1e from source. A few notes: My playground is an iOS Playground, and my schema is NOT defined directly in my playground as @bitserf tried, rather it is defined as a framework that I made, which I then import into the playground.

However, I have the caveat that I can only load a Realm database in read only mode. If I set readOnly to false, I get the error:

*** Terminating app due to uncaught exception 'RLMException', reason: 'open() failed: Operation not permitted'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102f0df35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104a2bbb7 objc_exception_throw + 45
    2   RealmDatabase                       0x000000010e989d28 _Z18RLMSetErrorOrThrowP7NSErrorPU15__autoreleasingS0_ + 264
    3   RealmDatabase                       0x000000010e96dc51 -[RLMRealm initWithPath:key:readOnly:inMemory:dynamic:error:] + 5201
    4   RealmDatabase                       0x000000010e96f80a +[RLMRealm realmWithPath:key:readOnly:inMemory:dynamic:schema:error:] + 2890
    5   RealmDatabase                       0x000000010e96e978 +[RLMRealm realmWithPath:readOnly:error:] + 152
    6   ???                                 0x000000010ef3740e 0x0 + 4545803278
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Note that RealmDatabase is my own framework that I build that I mentioned above.

I am loading the Realm file from the Playground's resource directory. I'm not sure how much not being able to write to the Realm is an issue with Realm, or a restriction with Playgrounds.

@donald-pinckney
Copy link

Update: I changed reading from the Playground resources to:

let path : String = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true)[0] as String).stringByAppendingPathComponent("realm.realm")

and now I can open the Realm file in writable mode.

@jpsim jpsim changed the title Crash when using Realm in Swift playground Support using Realm in Swift Playgrounds May 27, 2015
@zhigang1992
Copy link

How did you guys to it to import, mine is failing on import Realm or import RealmSwift

Playground execution failed: <module-includes>:1:9: note: in file included from <module-includes>:1:
#import "/Users/kylefang/Library/Developer/Xcode/DerivedData/Pomotodo2-gqvmkihjmtjsntanzzshgbrsykbc/Build/Products/Debug-iphonesimulator/Realm.framework/Headers/Realm.h"
        ^
/Users/kylefang/Library/Developer/Xcode/DerivedData/Pomotodo2-gqvmkihjmtjsntanzzshgbrsykbc/Build/Products/Debug-iphonesimulator/Realm.framework/Headers/Realm.h:21:9: error: 'Realm/RLMArray.h' file not found
#import <Realm/RLMArray.h>
        ^
could not build Objective-C module 'Realm'<module-includes>:2:9: note: in file included from <module-includes>:2:
#import "Headers/RealmSwift-Swift.h"
        ^

I'm on Xcode7 right now...

Added Realm with CocoaPods:

pod 'Realm', :git => 'https://github.com/realm/realm-cocoa.git', :branch => 'swift-2.0'
pod 'RealmSwift', :git => 'https://github.com/realm/realm-cocoa.git', :branch => 'swift-2.0'

@DanielAsher
Copy link

I found I could CRUD Realm databases within a playground by building 17972cb from source, placing the project file into a workspace, adding a playground and creating the realm in the 'Shared Playground Data' folder.

let realmPath = "/Users/Daniel/Documents/Shared Playground Data/CPWD.realm"

let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
    if (oldSchemaVersion < 1) {
    }
  }

let config = Realm.Configuration(path: realmPath, schemaVersion: 2, migrationBlock: migrationBlock )

let realm = try! Realm(configuration: config)

However, I then placed the realm database file and the required classes into a framework, #importd the framework, set to readOnly: true but ran into this error:
Error Domain=io.realm Code=1 "Operation not permitted"

with readOnly: false the error is:
Error Domain=io.realm Code=1 "open() failed: Operation not permitted"

Using playgrounds with Realm is fabulously useful. I hope this becomes a supported scenario. I've created all my realms this way. Would love to distribute in a framework for REPL exploration of the data...

👍

@icanzilb
Copy link
Contributor

icanzilb commented Mar 4, 2016

Realm works fabulously for me in a Playground both in-memory and on disc with the default configuration. I just crated a workspace with a playground and included RealmSwift 0.98.3 via Cocoapods by doing pod playgrounds RealmSwift and it worked straight away.

@DanielAsher
Copy link

@icanzilb this works for me too. I haven't analysed the conditions for success and still get scary looking warnings in the console, but REPL-oriented development is definitely possible with Realm !

@icanzilb
Copy link
Contributor

icanzilb commented Mar 5, 2016

@DanielAsher do you get any particular error reported? I have just this in the console, which looks like nothing serious:

2016-03-05 08:44:13.591 RealmSwift[40751:650775] Failed to obtain sandbox extension for path=/var/folders/64/vmn77sp112xdn5np6mjtwxfw0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.RealmSwift-18C8513C-641D-4EF0-AA8D-570FDCB2DEC4/Library/Caches/com.apple.dt.playground.stub.iOS_Simulator.RealmSwift-18C8513C-641D-4EF0-AA8D-570FDCB2DEC4. Errno:1

@DanielAsher
Copy link

@icanzilb - yes, this is (scary but harmless) error message I get. Realm REPL remains awesome.

@icanzilb
Copy link
Contributor

icanzilb commented Mar 7, 2016

ok it looks like there are subtle differences between running in a playground and in an app.

This code works in an app but crashes in a Playground:

let wife = Person()
let husband = Person()
husband.spouse = wife
wife.spouse = husband

try! realm.write {
    realm.add(husband)
}

While this code works in both an app and a Playground:

let wife = Person()
let husband = Person()
husband.spouse = wife

try! realm.write {
    realm.add(husband)
}

try! realm.write {
    wife.spouse = husband
}

I'm sure this will give a good hint if anyone's to fix this

@icanzilb
Copy link
Contributor

At present there might be more issues than I initially hoped for - trying to delete any objects also ends with SIGABRT. That's a pitty having realm in a playground could've been a real game changer

@icanzilb
Copy link
Contributor

Ok - got help and turns out both above issues were my own fault. So far I have zero issues running in a playground

@jpsim
Copy link
Contributor

jpsim commented Mar 29, 2016

As best we can tell, Realm Swift is very usable in Xcode Playgrounds, and we're adding an example in #3370. We just forgot to update this issue whenever this was resolved, likely many months ago 😱.

@jpsim jpsim closed this as completed Mar 29, 2016
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants