Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit dc05b6c

Browse files
committed
Merge pull request #1 from mtrovilho/model-refactor
little refactoring :)
2 parents fd36831 + 0808470 commit dc05b6c

File tree

5 files changed

+247
-149
lines changed

5 files changed

+247
-149
lines changed

.gitignore

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Created by https://www.gitignore.io/api/osx,xcode,swift
2+
3+
### OSX ###
4+
.DS_Store
5+
.AppleDouble
6+
.LSOverride
7+
8+
# Icon must end with two \r
9+
Icon
10+
11+
12+
# Thumbnails
13+
._*
14+
15+
# Files that might appear in the root of a volume
16+
.DocumentRevisions-V100
17+
.fseventsd
18+
.Spotlight-V100
19+
.TemporaryItems
20+
.Trashes
21+
.VolumeIcon.icns
22+
23+
# Directories potentially created on remote AFP share
24+
.AppleDB
25+
.AppleDesktop
26+
Network Trash Folder
27+
Temporary Items
28+
.apdisk
29+
30+
31+
### Xcode ###
32+
# Xcode
33+
#
34+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
35+
36+
## Build generated
37+
build/
38+
DerivedData
39+
40+
## Various settings
41+
*.pbxuser
42+
!default.pbxuser
43+
*.mode1v3
44+
!default.mode1v3
45+
*.mode2v3
46+
!default.mode2v3
47+
*.perspectivev3
48+
!default.perspectivev3
49+
xcuserdata
50+
51+
## Other
52+
*.xccheckout
53+
*.moved-aside
54+
*.xcuserstate
55+
56+
57+
### Swift ###
58+
# Xcode
59+
#
60+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
61+
62+
## Build generated
63+
build/
64+
DerivedData
65+
66+
## Various settings
67+
*.pbxuser
68+
!default.pbxuser
69+
*.mode1v3
70+
!default.mode1v3
71+
*.mode2v3
72+
!default.mode2v3
73+
*.perspectivev3
74+
!default.perspectivev3
75+
xcuserdata
76+
77+
## Other
78+
*.xccheckout
79+
*.moved-aside
80+
*.xcuserstate
81+
*.xcscmblueprint
82+
83+
## Obj-C/Swift specific
84+
*.hmap
85+
*.ipa
86+
87+
# CocoaPods
88+
#
89+
# We recommend against adding the Pods directory to your .gitignore. However
90+
# you should judge for yourself, the pros and cons are mentioned at:
91+
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
92+
#
93+
# Pods/
94+
95+
# Carthage
96+
#
97+
# Add this line if you want to avoid checking in source code from Carthage dependencies.
98+
# Carthage/Checkouts
99+
100+
Carthage/Build
101+

ListaCoreData.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
9646CAF61B8E8B60003FF188 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9646CAF51B8E8B60003FF188 /* Images.xcassets */; };
1515
9646CAF91B8E8B60003FF188 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9646CAF71B8E8B60003FF188 /* LaunchScreen.xib */; };
1616
9646CB051B8E8B61003FF188 /* ListaCoreDataTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9646CB041B8E8B61003FF188 /* ListaCoreDataTests.swift */; };
17-
96D974FA1B8F4193008C4F72 /* ListaCoreDataFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D974F91B8F4193008C4F72 /* ListaCoreDataFunctions.swift */; };
1817
96D974FC1B8F4212008C4F72 /* Itens.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D974FB1B8F4212008C4F72 /* Itens.swift */; };
1918
96EB560B1B8F61240059EB1B /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 96EB560A1B8F61240059EB1B /* README.md */; };
2019
/* End PBXBuildFile section */
@@ -41,7 +40,6 @@
4140
9646CAFE1B8E8B61003FF188 /* ListaCoreDataTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ListaCoreDataTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4241
9646CB031B8E8B61003FF188 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4342
9646CB041B8E8B61003FF188 /* ListaCoreDataTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListaCoreDataTests.swift; sourceTree = "<group>"; };
44-
96D974F91B8F4193008C4F72 /* ListaCoreDataFunctions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListaCoreDataFunctions.swift; sourceTree = "<group>"; };
4543
96D974FB1B8F4212008C4F72 /* Itens.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Itens.swift; sourceTree = "<group>"; };
4644
96EB560A1B8F61240059EB1B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
4745
/* End PBXFileReference section */
@@ -86,7 +84,6 @@
8684
9646CAE81B8E8B60003FF188 /* ListaCoreData */ = {
8785
isa = PBXGroup;
8886
children = (
89-
96D974F91B8F4193008C4F72 /* ListaCoreDataFunctions.swift */,
9087
96D974FB1B8F4212008C4F72 /* Itens.swift */,
9188
9646CAEB1B8E8B60003FF188 /* AppDelegate.swift */,
9289
9646CAF01B8E8B60003FF188 /* ViewController.swift */,
@@ -228,7 +225,6 @@
228225
9646CAF11B8E8B60003FF188 /* ViewController.swift in Sources */,
229226
96D974FC1B8F4212008C4F72 /* Itens.swift in Sources */,
230227
9646CAEF1B8E8B60003FF188 /* ListaCoreData.xcdatamodeld in Sources */,
231-
96D974FA1B8F4193008C4F72 /* ListaCoreDataFunctions.swift in Sources */,
232228
9646CAEC1B8E8B60003FF188 /* AppDelegate.swift in Sources */,
233229
);
234230
runOnlyForDeploymentPostprocessing = 0;

ListaCoreData/Itens.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import Foundation
1010
import CoreData
1111

1212
// MODEL para abstração dos dados
13+
// TODO: refatorar nome para Item, os Models devem ser vistos como entidades
14+
// (objetos) e não tabelas de db
1315
class Itens: NSManagedObject {
1416

1517
/// mapeamento do campo "nome" para String
@@ -19,6 +21,7 @@ class Itens: NSManagedObject {
1921
///
2022
/// :param: objectContext Conexão com o CoreData
2123
/// :param: nome Nome para salvar
24+
///
2225
class func criaNovoObjeto(objectContext: NSManagedObjectContext, nome: String) -> Itens {
2326
let newItem = NSEntityDescription.insertNewObjectForEntityForName("Itens", inManagedObjectContext: objectContext) as! Itens
2427

@@ -27,4 +30,76 @@ class Itens: NSManagedObject {
2730

2831
return newItem
2932
}
33+
34+
/// Função para buscar todos os dados do CoreData
35+
///
36+
/// :param: managedObjectContext contexto a ser manipulado
37+
///
38+
class func buscarTodos(managedObjectContext: NSManagedObjectContext) -> [Itens] {
39+
let listagemCoreData = NSFetchRequest(entityName: "Itens")
40+
41+
// ordena os resultados alfabetica pelo campo "nome"
42+
let orderByName = NSSortDescriptor(key: "nome", ascending: true, selector: "caseInsensitiveCompare:")
43+
44+
// define o filtro de ordenação
45+
listagemCoreData.sortDescriptors = [orderByName]
46+
47+
// executa a busca e retorna os valores no array itens
48+
return managedObjectContext.executeFetchRequest(listagemCoreData, error: nil) as? [Itens] ?? []
49+
}
50+
51+
/// Função para encontrar um Item por nome
52+
///
53+
/// :param: nome Nome do item para buscar
54+
/// :param: managedObjectContext contexto a ser manipulado
55+
///
56+
class func buscar(nome: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Itens? {
57+
let fetchRequest = NSFetchRequest(entityName: "Itens")
58+
fetchRequest.predicate = NSPredicate(format: "nome = %@", nome)
59+
60+
let result = managedObjectContext.executeFetchRequest(fetchRequest, error: nil) as? [Itens]
61+
return result?.first
62+
}
63+
64+
/// Função para verificar se já existe um registro com esse nome
65+
///
66+
/// :param: nome Nome do item para buscar
67+
/// :param: managedObjectContext contexto a ser manipulado
68+
///
69+
class func itemExistente(nome: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> Bool {
70+
return buscar(nome, inManagedObjectContext: managedObjectContext) != nil
71+
}
72+
73+
// MARK: - Init
74+
75+
convenience init(nome: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) {
76+
let entity = NSEntityDescription.entityForName("Itens", inManagedObjectContext: managedObjectContext)!
77+
self.init(entity: entity, insertIntoManagedObjectContext: managedObjectContext)
78+
self.nome = nome
79+
}
80+
81+
// MARK: -
82+
83+
/// Função para apagar um item do CoreData
84+
///
85+
/// :param: managedObjectContext contexto a ser manipulado
86+
///
87+
func apagar(managedObjectContext: NSManagedObjectContext) {
88+
managedObjectContext.deleteObject(self)
89+
}
90+
91+
/// Função para salvar os dados no CoreData
92+
///
93+
/// :param: managedObjectContext contexto a ser manipulado
94+
///
95+
func salvar(managedObjectContext: NSManagedObjectContext) {
96+
var error: NSErrorPointer = nil
97+
98+
// se a ação salvar falhar, imprime erro
99+
if(managedObjectContext.save(error)) {
100+
if (error != nil) {
101+
println("Erro ao salvar: \(error.debugDescription)")
102+
}
103+
}
104+
}
30105
}

ListaCoreData/ListaCoreDataFunctions.swift

Lines changed: 0 additions & 116 deletions
This file was deleted.

0 commit comments

Comments
 (0)