Should I Use Realm Objective-C or Realm Swift in My Mixed Objective-C/Swift Project

Should I use Realm Objective-C or Realm Swift in my mixed Objective-C / Swift project?

They key detail is whether you intend to access your Realm model classes from only Swift, or both Swift and Objective-C. If you will only access them from Swift you can happily use Realm Swift, even if elsewhere in your application you use code written in Objective-C. If you will access your Realm model classes from both Swift and Objective-C code you'll need to use Realm Objective-C rather than Realm Swift.

Using Charts with Realm in Objective-C

A problem is not mixing Realm and RealmSwift. You seems install Realm without Charts first, then add Charts to the Podfile then pod install, right? So the Podfile.lock (a file pinning library to specific version) pinned Realm to 2.0.0 but Charts requires older version of RealmSwift (RealmSwift depends on Realm implicitely). That is the cause of the error.

To resolve that, you can execute pod update or just delete pod 'Realm' from the Podfile. Either way remove Realm 2.0 then install Realm and RealmSwift 1.1.0 (1.1.0 is Chart's required).

You cannot use higher version of Realm because Charts requres 1.1.0.

installing RealmSwift pod also installs Realm (ObjC)?

RealmSwift is a wrapper around existing code in Objective-C++. As Swift is still a "work in progress" and Realm has a very large codebase, it is more convenient to start wrapping the older code around and give the Swift features out to the developers without having to redo everything from scratch.

I do not know what is the plan for future, but the contributors to Realm taught this concept in a workshop:

In this workshop, you'll create a Swift wrapper framework around a small existing Objective-C library.

To answer your question - no. The Swift wrapper is there to support Swift features, but really only calls the older functions with some other work around it to give it the Swifty feeling.

Use Realm data to fill the UITableView (table)?

The following code should work for you:

// ViewController.h
#import <UIKit/UIKit.h>
#import "MyObject.h" // subclass of RLMObject

@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property RLMResults<MyObject *> *objectsList;

@end

And the implementation:

// ViewController.m
#import "MyViewController.h"

@implementation MyViewController {

- (void)viewDidLoad {
_objectsList = [MyObject allObjects]; // optionally sort, query, etc.
}

- (void)viewDidAppear {
[super viewDidAppear];
// Do any other setup
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _objectsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Setup the cell with the object's information. It's up to you how
// to do it. Register a custom subclass of UITableViewCell, for example.
MyObject *thisCellObject = [objectsList objectAtIndex:indexPath.row];
UITableViewCell *cell = [self.myTableView dequeueReusableCellWithIdentifier:@"customIdentifier"];

// Title should be a property on your MyObject class
cell.textLabel.text = thisCellObject.title;
}

#pragma mark - UITableViewDelegate

// Implement the Delegate methods as you want it...
}

No need to invoke the Realm Objects from other threads of using Dispatch. The only case you'll get those "incorrect thread" errors would be if you pass a reference from you view controller to other class, like a background networking client or something similar.

Anyway those cases are avoidable too. If your objects got an ID, let's say from an API, you pass the ID to the other class, maybe the background sync client, and from that client perform a query for the object, like [MyObject objectsWhere:@"objectID == %@", idString];. The object is being called from the background process that will use it and therefore it's not going to fail.

Swift 3.0: Type 'RLMResultsRLMObjectType' does not conform to protocol 'Sequence'

How can I get RLMResults to conform to Sequence?

See Using Realm Objective-C from Swift for information on how to make Realm Objective-C's types work more naturally from Swift. In particular, take note of the section on RLMSupport.swift:

We recommend you compile the Swift/RLMSupport.swift file (which is also available in our release zip).

This file adds SequenceType conformance to Realm Objective‑C collection types and re-exposes Objective‑C methods that aren’t natively accessible from Swift like methods including variadic arguments.

Realm Objective‑C doesn’t include this file by default because that would force all users of Realm Objective‑C to include the hefty Swift dynamic libraries regardless of whether or not they use Swift in their app!

However, due to a bug in the Swift 3 compiler the conformance of RLMResults and RLMArray to Sequence has been temporarily disabled. Hopefully the Swift compiler issue will be resolved prior to Xcode 8 moving out of beta.

Where I can find Realm with support for Swift 3?

Support for Swift 3 with Realm Swift is present on Realm's master branch, alongside the existing support for Swift 2. It will become available in releases of Realm Swift once Xcode 8 and Swift 3 are finalized, later this year.



Related Topics



Leave a reply



Submit