public void bulkInsert ( final List < T > objects , final SuccessCallback < List < Long > > successCallback , final ErrorCallback errorCallback ) { checkNotNull ( successCallback ) ; checkNotNull ( errorCallback ) ; checkNotNull ( objects ) ; final Callable < List < Long > > call = new Callable < List < Long > > ( ) { @ Override public List < Long > call ( ) throws Exception { List < Long > ids = new ArrayList <> ( objects . size ( ) ) ; for ( int i = 0 ; i < objects . size ( ) ; i ++ ) { Long id = SugarRecord . save ( objects . get ( i ) ) ; ids . add ( i , id ) ; } return ids ; } } ; final Future < List < Long > > future = doInBackground ( call ) ; List < Long > ids ; try { ids = future . get ( ) ; if ( null == ids || ids . isEmpty ( ) ) { errorCallback . onError ( new Exception ( "Error when performing bulk insert" ) ) ; } else { successCallback . onSuccess ( ids ) ; } } catch ( Exception e ) { errorCallback . onError ( e ) ; } }
Method that performs a bulk insert . It works on top of SugarRecord class and executes the query asynchronously using Futures .