パパエンジニアのアウトプット帳

30歳に突入した1児のパパエンジニアのブログ

【Swift初心者】TableViewで無限スクロール

TableViewを無限スクロールしたかった。


本当はライブラリないか探していたんだけど、有名どころはない感じかな?

実装方法がそんなに難しくなさそうなので、自分で実装してみた。


実装は下記のサイトたちを参考にしてscrollViewDidScrollでやった。

qiita.com

github.com


実装してみると初期表示時にも呼ばれてるっぽかった・・・ なので、下記のサイトのようにtableView.draggingで判定した。

qiita.com


でも、これだと一度に複数回APIが呼ばれていたので、読み込み中フラグ持たせて読み込み中に二重でAPIが呼ばれないようにしてみた。

func scrollViewDidScroll(scrollView: UIScrollView) {
      if (nowLoading) {
          return
      }

      if (tableView.dragging) {
          let currentOffset = scrollView.contentOffset.y
          let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

          if (maximumOffset - currentOffset) <= 40 {
              let next_page = (items.count / pageSize) + 1
              loadList(next_page)
          }
      }
  }

MecabのDocker包み ~Web APIを添えて~

カッコつけただけです。すいません。


Mecabをちょっと使いたいなと思ったけど、ローカルにインストールするの面倒なのでDockerで作ろうと考えた。

でも、Rubyから使ったりしたいとローカルにMecabインストールしないと・・・

と、思って調べていたら同じようなことを考えている人がいたようで。

qiita.com

https://hub.docker.com/r/bungoume/mecab-web-api/


せっかくなので利用させてもらおう。


ユーザ辞書の追加をしてる。

日本酒に特化したものが下記にあったので試しに入れてみた。

※後からmecab-ipadic-neologd入れたので、おそらくこれなくてもいいと思う

x1.inkenkun.com

Dockerfile

MecabをWeb API利用できるDockerコンテナ


docker build -t mecab-web-api .でビルドして

docker run -d -p 8000:80 -t mecab-web-api webmecabで起動すれば使える。


何かエラー出たりして詰まったら、とりあえず下記の方法でコンテナに入って調べればいいと思う。

qiita.com

ユーザ辞書とシステム辞書の作成・追加

必要になった場合は下記を参考にすればよさそう。

blog.apar.jp

Sorceryでアレやりたい場合メモ

Railsでユーザ認証にDeviseではなくSorcery使ってる場合にアレ(招待機能とか)どうやるの?の参考サイトメモ。

招待機能

qiita.com

ユーザ名でログイン

dev.classmethod.jp

OAuth

qiita.com

APIサーバー認証

nirasan.hatenablog.com

番外編(2016/08/19 追加)

qiita.com

dev.classmethod.jp

dependent: :restrict_with_errorのI18n対応にちょっとつまずいた

dependent: :restrict_with_errorを利用した時に、エラーメッセージを日本語化しようとしてちょっと調べたのでメモとして残す。

ちなみに、dependentオプションについては下記がまとまってて分かりやすい。

qiita.com


例えば下記のようなモデルがあったとする。

class User < ActiveRecord::Base
  belongs_to :department
end

class Department < ActiveRecord::Base
  has_many :users, dependent: :restrict_with_error
end

restrict_with_errorのlocaleの書き方ははググればすぐ出てくるが、このrecordの部分が日本語にならなくて困った。。

[translation_ja.yml]
ja:
  activerecord:
    errors:
      messages:
        restrict_dependent_destroy:
          many: "%{record}が存在しているため削除できません。"

モデル名のI18nなので下記のように書けばいけると思っていたが、どうもusersと複数系になっているので違う(usersにもしてみたがダメ)

[translation_ja.yml]
  activerecord:
    models:
      user: "ユーザ"

コード見ていると下記のようになっている。

# File activerecord/lib/active_record/associations/has_many_association.rb, line 11
      def handle_dependency
        case options[:dependent]
        when :restrict_with_exception
          raise ActiveRecord::DeleteRestrictionError.new(reflection.name) unless empty?

        when :restrict_with_error
          unless empty?
            record = klass.human_attribute_name(reflection.name).downcase
            owner.errors.add(:base, :"restrict_dependent_destroy.many", record: record)
            false
          end

        else
          if options[:dependent] == :destroy
            # No point in executing the counter update since we're going to destroy the parent anyway
            load_target.each { |t| t.destroyed_by_association = reflection }
            destroy_all
          else
            delete_all
          end
        end
      end

whenのrestrict_with_error部分にklass.human_attribute_name(reflection.name).downcaseとある。

human_attribute_nameだと!?

なので下記のように対応したら日本語表示されました。

[translation_ja.yml]
  activerecord:
    attributes:
      user:
        users: ユーザ


なんか気持ち悪い。。。