Skip to content

Commit

Permalink
Bug fix: respondsToSelector won't work here, so switching to a workar…
Browse files Browse the repository at this point in the history
…ound. Addresses issue #515
  • Loading branch information
robbiehanson committed Apr 17, 2020
1 parent b3d4599 commit dda1ece
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 38 deletions.
30 changes: 28 additions & 2 deletions Testing/Swift-desktop/Swift-desktop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import YapDatabase
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// self.testDatabase()
self.testUpgrade()
// testDatabase()
// testUpgrade()
testIssue515()
}

private func testDatabase() {
Expand Down Expand Up @@ -82,4 +83,29 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
}

private func testIssue515() {

let baseDirs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let baseDir = baseDirs[0]

let databaseURL = baseDir.appendingPathComponent("database.sqlite")

let database = YapDatabase(url: databaseURL)

let collection = "issue515"
database?.registerCodableSerialization(Issue515.self, forCollection: collection)

let ext = YapDatabaseRelationship()
database?.register(ext, withName: "relationships")

let databaseConnection = database?.newConnection()

databaseConnection?.asyncReadWrite {(transaction) in

let test = Issue515(foobar: 42)

transaction.setObject(test, forKey: "key", inCollection: collection)
}
}
}
4 changes: 3 additions & 1 deletion Testing/Swift-desktop/Swift-desktop/Issue515.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Foundation
import YapDatabase

struct Issue515: YapDatabaseRelationshipNode {
struct Issue515: Codable, YapDatabaseRelationshipNode {

let foobar: Int

func yapDatabaseRelationshipEdges() -> [YapDatabaseRelationshipEdge]? {

print("swift called")
return nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NS_ASSUME_NONNULL_BEGIN

/**
* See YapDatabaseRelationship.swift file for implementation of bridge.
*/
@protocol YapDatabaseRelationshipTransaction_SwiftBridge

- (nullable NSArray<YapDatabaseRelationshipEdge *> *)swiftBridge_yapDatabaseRelationshipEdges:(id)obj;

@end

NS_ASSUME_NONNULL_End

This comment has been minimized.

Copy link
@hujunfeng

hujunfeng Apr 18, 2020

This header file cannot be parsed by the compiler. Found a typo here: _End -> _END.

This comment has been minimized.

Copy link
@robbiehanson

robbiehanson Apr 20, 2020

Author Contributor

That's odd that my compiler didn't catch this. Pushed another commit to this branch that fixes it.

Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/// Add Swift extensions here

extension YapDatabaseRelationshipTransaction {

@objc func swiftBridge_yapDatabaseRelationshipEdges(_ obj: AnyObject) -> [YapDatabaseRelationshipEdge]? {

var edges: [YapDatabaseRelationshipEdge]? = nil
if let node = obj as? YapDatabaseRelationshipNode {
edges = node.yapDatabaseRelationshipEdges()
}

return edges
}
}

/// The objective-c protocol is automatically imported as a class-only protocol.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "YapDatabaseRelationshipTransaction.h"
#import "YapDatabaseRelationshipBridge.h"
#import "YapDatabaseRelationshipPrivate.h"
#import "YapDatabaseRelationshipEdgePrivate.h"
#import "YapDatabasePrivate.h"
Expand Down Expand Up @@ -508,18 +509,16 @@ - (BOOL)populateTable
void (^ProcessRow)(int64_t rowid, NSString *collection, NSString *key, id object);
ProcessRow = ^(int64_t rowid, NSString *collection, NSString *key, id object){

NSArray *givenEdges = nil;
NSArray<YapDatabaseRelationshipEdge *> *givenEdges = nil;

// We cannot use the conformsToProtocol check anymore.
// This is because we have 2 different protocols now.
// One for objective-c, and another for swift.
// So we need to use the respondsToSelector check instead, as it supports either language.
//
// if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
if ([object respondsToSelector:@selector(yapDatabaseRelationshipEdges)])
if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
{
givenEdges = [object yapDatabaseRelationshipEdges];
}
else if ([self respondsToSelector:@selector(swiftBridge_yapDatabaseRelationshipEdges:)])
{
givenEdges = [(id)self swiftBridge_yapDatabaseRelationshipEdges:object];
}

if ([givenEdges count] > 0)
{
Expand Down Expand Up @@ -4005,22 +4004,20 @@ - (void)didInsertObject:(id)object

// Request edges from object

NSArray *givenEdges = nil;
NSArray<YapDatabaseRelationshipEdge *> *givenEdges = nil;

// We cannot use the conformsToProtocol check anymore.
// This is because we have 2 different protocols now.
// One for objective-c, and another for swift.
// So we need to use the respondsToSelector check instead, as it supports either language.
//
// if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
if ([object respondsToSelector:@selector(yapDatabaseRelationshipEdges)])
if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
{
givenEdges = [object yapDatabaseRelationshipEdges];
}
else if ([self respondsToSelector:@selector(swiftBridge_yapDatabaseRelationshipEdges:)])
{
givenEdges = [(id)self swiftBridge_yapDatabaseRelationshipEdges:object];
}

// Make copies, and fill in missing src information

NSMutableArray *edges = nil;
NSMutableArray<YapDatabaseRelationshipEdge *> *edges = nil;

if (givenEdges.count > 0)
{
Expand Down Expand Up @@ -4077,20 +4074,18 @@ - (void)didUpdateObject:(id)object

// Request edges from object

NSArray *givenEdges = nil;
NSArray<YapDatabaseRelationshipEdge *> *givenEdges = nil;

// We cannot use the conformsToProtocol check anymore.
// This is because we have 2 different protocols now.
// One for objective-c, and another for swift.
// So we need to use the respondsToSelector check instead, as it supports either language.
//
// if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
if ([object respondsToSelector:@selector(yapDatabaseRelationshipEdges)])
if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
{
givenEdges = [object yapDatabaseRelationshipEdges];
}
else if ([self respondsToSelector:@selector(swiftBridge_yapDatabaseRelationshipEdges:)])
{
givenEdges = [(id)self swiftBridge_yapDatabaseRelationshipEdges:object];
}

NSMutableArray *edges = nil;
NSMutableArray<YapDatabaseRelationshipEdge *> *edges = nil;

if (givenEdges.count > 0)
{
Expand Down Expand Up @@ -4138,20 +4133,18 @@ - (void)didReplaceObject:(id)object forCollectionKey:(YapCollectionKey *)collect
return;
}

NSArray *givenEdges = nil;
NSArray<YapDatabaseRelationshipEdge *> *givenEdges = nil;

// We cannot use the conformsToProtocol check anymore.
// This is because we have 2 different protocols now.
// One for objective-c, and another for swift.
// So we need to use the respondsToSelector check instead, as it supports either language.
//
// if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
if ([object respondsToSelector:@selector(yapDatabaseRelationshipEdges)])
if ([object conformsToProtocol:@protocol(YapDatabaseRelationshipNode)])
{
givenEdges = [object yapDatabaseRelationshipEdges];
}
else if ([self respondsToSelector:@selector(swiftBridge_yapDatabaseRelationshipEdges:)])
{
givenEdges = [(id)self swiftBridge_yapDatabaseRelationshipEdges:object];
}

NSMutableArray *edges = nil;
NSMutableArray<YapDatabaseRelationshipEdge *> *edges = nil;

if (givenEdges)
{
Expand Down

0 comments on commit dda1ece

Please sign in to comment.