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

bundle moved to model #26

Merged
merged 4 commits into from
Jun 27, 2019
Merged

bundle moved to model #26

merged 4 commits into from
Jun 27, 2019

Conversation

jaimejiyepark
Copy link
Collaborator

No description provided.

BundleURL.swift Outdated
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
class var BundleURL: URL{
/// URL of model assuming it was installed in the same bundle as this class
let documentDirectory = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still a !

BundleURL.swift Outdated
import CoreML

@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
class var BundleURL: URL{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this a struct and have a static method called bundleUrl(modelName: String) -> URL? or something like that

BundleURL.swift Outdated
let modelcFile = documentDirectory.appendingPathComponent("FindFour.mlmodelc")

if !FileManager.default.fileExists(atPath: modelcFile.path) {
if let bundleUrl = Bundle(for: FindFour.self).url(forResource: "CardScan", withExtension: "bundle"){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using

guard if let bundleUrl = Bundle(for: ...) else {
    print("could not get bundle url")
    return nil
}

guard if let bundle = Bundle(url: bundleUrl) else {
    return nil
}

and so on

BundleURL.swift Outdated
if !FileManager.default.fileExists(atPath: modelcFile.path) {
if let bundleUrl = Bundle(for: FindFour.self).url(forResource: "CardScan", withExtension: "bundle"){
if let bundle = Bundle(url: bundleUrl){
if let modelUrl = bundle.url(forResource: "FindFour", withExtension: "bin"){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here with guard, we should try to avoid the if waterfall

BundleURL.swift Outdated
@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
//model name : "FindFour.mlmodelc"
struct BundleURL{
static func bundleUrl(modelName: String) -> URL?{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between ?{

BundleURL.swift Outdated
//model name : "FindFour.mlmodelc"
struct BundleURL{
static func bundleUrl(modelName: String) -> URL?{
var modelcFile: URL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this a var? Can it be a let

BundleURL.swift Outdated
struct BundleURL{
static func bundleUrl(modelName: String) -> URL?{
var modelcFile: URL
if let documentDirectory =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use a guard here also since we're always going to need this directory and if we can get it we can't compile

BundleURL.swift Outdated

let modelUrl = bundle.url(forResource: "FindFour", withExtension: "bin")

if let compiledUrl =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use a guard here or return nil on the else path because the only way we will return a non null is if everything in this function works

@@ -99,7 +84,7 @@ class FindFour {

/// Construct a model that automatically loads the model from the app's bundle
convenience init() {
try! self.init(contentsOf: type(of:self).urlOfModelInThisBundle)
try! self.init(contentsOf: BundleURL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this still? We can just instantiate the class directly using the url we get from your static method

Copy link
Collaborator

@kingst kingst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment, but #shipit 🚢

try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) {
modelcFile = documentDirectory.appendingPathComponent(modelName)
}
static func bundleUrl(forResource: String, withExtension: String, modelName: String) -> URL?{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment, but I'd make forResource and withExtension the public variable names with something like resource and extension internally

@kingst kingst merged commit 15f7737 into master Jun 27, 2019
@kingst kingst deleted the move_bundle branch June 27, 2019 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants